Changeset 781 for soft


Ignore:
Timestamp:
Feb 7, 2016, 7:47:38 PM (8 years ago)
Author:
alain
Message:

Fixing two bugs:
1) In the string library: the strcpy() function must copy
the terminating NUL character in the destination buffer.
2) In the malloc library: the free() function must reset
the entry associated to the released buffer in the alloc[array].

Location:
soft/giet_vm/giet_libs
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • soft/giet_vm/giet_libs/malloc.c

    r777 r781  
    412412    }
    413413
     414    // reset the alloc[index] entry
     415    *pchar = 0;
     416
    414417    // call the recursive function update_free_array()
    415418    update_free_array( &heap[x][y], base, size_index );
  • soft/giet_vm/giet_libs/malloc.h

    r777 r781  
    1212// Free blocks organisation:
    1313// - All free blocks have a size that is a power of 2, larger or equal
    14 //   to MIN_BLOCK_SIZE (typically 128 bytes).
     14//   to MIN_BLOCK_SIZE (typically 64 bytes).
    1515// - All free blocks are aligned.
    1616// - They are pre-classed in NB_SIZES linked lists, where all blocks in a
     
    4747//   of the allocated block must be obtained from the base address of the block. 
    4848// - The number of entries in this array is equal to the max number
    49 //   of allocated block is : heap_size / 128.
     49//   of allocated block is : heap_size / MIN_BLOCK_SIZE.
    5050// - For each allocated block, the value registered in the alloc[] array
    5151//   is log2( size_of_allocated_block ).
  • soft/giet_vm/giet_libs/string.c

    r777 r781  
    66///////////////////////////////////////////////////////////////////////////////////
    77
    8 ///////////////////////////////////////////////////////////////////////////////////
    9 // char * strcpy (char * destination, const char * source)
    10 ///////////////////////////////////////////////////////////////////////////////////
     8///////////////////////////////////////////////////////
    119char * strcpy (char * destination, const char * source)
    1210{
    13     if (!destination || !source) {
    14         return destination;
    15     }
     11    if (!destination || !source) return destination;
    1612
    17     while (*source) {
    18         *(destination++) = *(source++);
    19     }
     13    char* src = source;
     14    char* dst = destination;
     15    do
     16    {
     17        *dst = *src;
     18        dst++;
     19        src++;
     20    }  while (*src);
     21
     22    *dst = 0;
    2023
    2124    return destination;
     
    2326
    2427
    25 ///////////////////////////////////////////////////////////////////////////////////
    26 // char * strncpy (char * destination, const char * source, int maxlen)
    27 ///////////////////////////////////////////////////////////////////////////////////
     28////////////////////////////////////////////////////
    2829char * strncpy(char * dest, const char * src, int n)
    2930{
    3031    int i;
    31     for (i = 0; i < n && src[i] != '\0'; i++) {
     32    for (i = 0; i < n && src[i] != '\0'; i++)
     33    {
    3234        dest[i] = src[i];
    3335    }
    34     for (; i < n ; i++) {
     36    for (; i < n ; i++)
     37    {
    3538        dest[i] = '\0';
    3639    }
     40
    3741    return dest;
    3842}
    3943
    4044
    41 ///////////////////////////////////////////////////////////////////////////////////
    42 // int strcmp (const char * str1, const char * str2)
    43 ///////////////////////////////////////////////////////////////////////////////////
     45/////////////////////////////////////////////////
    4446int strcmp (const char * str1, const char * str2)
    4547{
    46     if (!str1 || !str2) {
    47         return -123456; // return a value out of the char's bounds
     48    if (!str1 || !str2)
     49    {
     50        return -123456;     // return a value out of the char's bounds
    4851    }
    4952
    50     while (*str1 && *str1 == *str2) {
     53    while (*str1 && *str1 == *str2)
     54    {
    5155        str1++;
    5256        str2++;
     
    5660}
    5761
    58 ///////////////////////////////////////////////////////////////////////////////////
    59 // int strlen ( const char * str )
    60 ///////////////////////////////////////////////////////////////////////////////////
     62/////////////////////////////
    6163int strlen (const char * str)
    6264{
    6365    const char * s = str;
    6466
    65     while (*s) {
     67    while (*s)
     68    {
    6669        s++;
    6770    }
     
    7073
    7174
    72 ///////////////////////////////////////////////////////////////////////////////////
    73 // char * strchr(const char * str)
    74 ///////////////////////////////////////////////////////////////////////////////////
     75//////////////////////////////////////
    7576char * strchr(const char * str, int c)
    7677{
    7778    const char * s = str;
    7879    const char ch = c;
    79     while (*s != ch && *s != '\0') {
     80    while (*s != ch && *s != '\0')
     81    {
    8082        s++;
    8183    }
  • soft/giet_vm/giet_libs/string.h

    r777 r781  
    1010
    1111////////////////////////////////////////////////////////////////////////////////////////
    12 // This function copies the source string to the dest string.
     12// This function copies the source string to the dest string, including the NUL
     13// terminating character.
    1314// It returns a pointer on the dest string.
    1415////////////////////////////////////////////////////////////////////////////////////////
     
    2324
    2425////////////////////////////////////////////////////////////////////////////////////////
    25 //The strcmp() function compares the two strings s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.
     26// The strcmp() function compares the two strings s1 and s2.  It returns an integer
     27// less than, equal to, or greater than zero if s1 is found, respectively, to be smaller
     28// to match, or be greater than s2.
    2629////////////////////////////////////////////////////////////////////////////////////////
    2730int strcmp(const char * str1, const char * str2);
Note: See TracChangeset for help on using the changeset viewer.