Changeset da68871a in mainline for uspace/lib/c/generic


Ignore:
Timestamp:
2012-08-08T08:46:22Z (13 years ago)
Author:
Adam Hraska <adam.hraska+hos@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
30c0826
Parents:
bc216a0 (diff), 1d01cca (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merged changes from mainline.

Location:
uspace/lib/c/generic
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/malloc.c

    rbc216a0 rda68871a  
    109109        (((uintptr_t) (area)->end) - sizeof(heap_block_foot_t))
    110110
     111#define AREA_LAST_BLOCK_HEAD(area) \
     112        ((uintptr_t) BLOCK_HEAD(((heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area))))
     113
    111114/** Get header in heap block.
    112115 *
     
    346349                return false;
    347350       
    348         /* Add new free block */
    349         size_t net_size = (size_t) (end - area->end);
    350         if (net_size > 0)
    351                 block_init(area->end, net_size, true, area);
     351        heap_block_head_t *last_head =
     352            (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
     353       
     354        if (last_head->free) {
     355                /* Add the new space to the last block. */
     356                size_t net_size = (size_t) (end - area->end) + last_head->size;
     357                malloc_assert(net_size > 0);
     358                block_init(last_head, net_size, true, area);
     359        } else {
     360                /* Add new free block */
     361                size_t net_size = (size_t) (end - area->end);
     362                if (net_size > 0)
     363                        block_init(area->end, net_size, true, area);
     364        }
    352365       
    353366        /* Update heap area parameters */
     
    355368       
    356369        return true;
    357 }
    358 
    359 /** Try to enlarge any of the heap areas
    360  *
    361  * Should be called only inside the critical section.
    362  *
    363  * @param size Gross size of item to allocate (bytes).
    364  *
    365  */
    366 static bool heap_grow(size_t size)
    367 {
    368         if (size == 0)
    369                 return true;
    370        
    371         /* First try to enlarge some existing area */
    372         for (heap_area_t *area = first_heap_area; area != NULL;
    373             area = area->next) {
    374                 if (area_grow(area, size))
    375                         return true;
    376         }
    377        
    378         /* Eventually try to create a new area */
    379         return area_create(AREA_OVERHEAD(size));
    380370}
    381371
     
    661651}
    662652
     653/** Try to enlarge any of the heap areas.
     654 *
     655 * If successful, allocate block of the given size in the area.
     656 * Should be called only inside the critical section.
     657 *
     658 * @param size  Gross size of item to allocate (bytes).
     659 * @param align Memory address alignment.
     660 *
     661 * @return Allocated block.
     662 * @return NULL on failure.
     663 *
     664 */
     665static void *heap_grow_and_alloc(size_t size, size_t align)
     666{
     667        if (size == 0)
     668                return NULL;
     669       
     670        /* First try to enlarge some existing area */
     671        for (heap_area_t *area = first_heap_area; area != NULL;
     672            area = area->next) {
     673               
     674                if (area_grow(area, size + align)) {
     675                        heap_block_head_t *first =
     676                            (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
     677                       
     678                        void *addr =
     679                            malloc_area(area, first, NULL, size, align);
     680                        malloc_assert(addr != NULL);
     681                        return addr;
     682                }
     683        }
     684       
     685        /* Eventually try to create a new area */
     686        if (area_create(AREA_OVERHEAD(size + align))) {
     687                heap_block_head_t *first =
     688                    (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(last_heap_area);
     689               
     690                void *addr =
     691                    malloc_area(last_heap_area, first, NULL, size, align);
     692                malloc_assert(addr != NULL);
     693                return addr;
     694        }
     695       
     696        return NULL;
     697}
     698
    663699/** Allocate a memory block
    664700 *
     
    679715       
    680716        size_t falign = lcm(align, BASE_ALIGN);
    681         size_t real_size = GROSS_SIZE(ALIGN_UP(size, falign));
    682        
    683         bool retry = false;
    684         heap_block_head_t *split;
    685        
    686 loop:
     717       
     718        /* Check for integer overflow. */
     719        if (falign < align)
     720                return NULL;
     721       
     722        /*
     723         * The size of the allocated block needs to be naturally
     724         * aligned, because the footer structure also needs to reside
     725         * on a naturally aligned address in order to avoid unaligned
     726         * memory accesses.
     727         */
     728        size_t gross_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
    687729       
    688730        /* Try the next fit approach */
    689         split = next_fit;
     731        heap_block_head_t *split = next_fit;
    690732       
    691733        if (split != NULL) {
    692                 void *addr = malloc_area(split->area, split, NULL, real_size,
     734                void *addr = malloc_area(split->area, split, NULL, gross_size,
    693735                    falign);
    694736               
     
    703745                    AREA_FIRST_BLOCK_HEAD(area);
    704746               
    705                 void *addr = malloc_area(area, first, split, real_size,
     747                void *addr = malloc_area(area, first, split, gross_size,
    706748                    falign);
    707749               
     
    710752        }
    711753       
    712         if (!retry) {
    713                 /* Try to grow the heap space */
    714                 if (heap_grow(real_size)) {
    715                         retry = true;
    716                         goto loop;
    717                 }
    718         }
    719        
    720         return NULL;
     754        /* Finally, try to grow heap space and allocate in the new area. */
     755        return heap_grow_and_alloc(gross_size, falign);
    721756}
    722757
     
    731766void *calloc(const size_t nmemb, const size_t size)
    732767{
     768        // FIXME: Check for overflow
     769       
    733770        void *block = malloc(nmemb * size);
    734771        if (block == NULL)
     
    870907        if (addr == NULL)
    871908                return;
    872 
     909       
    873910        futex_down(&malloc_futex);
    874911       
  • uspace/lib/c/generic/str.c

    rbc216a0 rda68871a  
    428428 *
    429429 * Do a char-by-char comparison of two NULL-terminated strings.
    430  * The strings are considered equal iff they consist of the same
    431  * characters on the minimum of their lengths.
     430 * The strings are considered equal iff their length is equal
     431 * and both strings consist of the same sequence of characters.
     432 *
     433 * A string S1 is less than another string S2 if it has a character with
     434 * lower value at the first character position where the strings differ.
     435 * If the strings differ in length, the shorter one is treated as if
     436 * padded by characters with a value of zero.
    432437 *
    433438 * @param s1 First string to compare.
    434439 * @param s2 Second string to compare.
    435440 *
    436  * @return 0 if the strings are equal, -1 if first is smaller,
    437  *         1 if second smaller.
     441 * @return 0 if the strings are equal, -1 if the first is less than the second,
     442 *         1 if the second is less than the first.
    438443 *
    439444 */
     
    466471 *
    467472 * Do a char-by-char comparison of two NULL-terminated strings.
    468  * The strings are considered equal iff they consist of the same
    469  * characters on the minimum of their lengths and the length limit.
     473 * The strings are considered equal iff
     474 * min(str_length(s1), max_len) == min(str_length(s2), max_len)
     475 * and both strings consist of the same sequence of characters,
     476 * up to max_len characters.
     477 *
     478 * A string S1 is less than another string S2 if it has a character with
     479 * lower value at the first character position where the strings differ.
     480 * If the strings differ in length, the shorter one is treated as if
     481 * padded by characters with a value of zero. Only the first max_len
     482 * characters are considered.
    470483 *
    471484 * @param s1      First string to compare.
     
    473486 * @param max_len Maximum number of characters to consider.
    474487 *
    475  * @return 0 if the strings are equal, -1 if first is smaller,
    476  *         1 if second smaller.
     488 * @return 0 if the strings are equal, -1 if the first is less than the second,
     489 *         1 if the second is less than the first.
    477490 *
    478491 */
     
    508521        return 0;
    509522
     523}
     524
     525/** Test whether p is a prefix of s.
     526 *
     527 * Do a char-by-char comparison of two NULL-terminated strings
     528 * and determine if p is a prefix of s.
     529 *
     530 * @param s The string in which to look
     531 * @param p The string to check if it is a prefix of s
     532 *
     533 * @return true iff p is prefix of s else false
     534 *
     535 */
     536bool str_test_prefix(const char *s, const char *p)
     537{
     538        wchar_t c1 = 0;
     539        wchar_t c2 = 0;
     540       
     541        size_t off1 = 0;
     542        size_t off2 = 0;
     543
     544        while (true) {
     545                c1 = str_decode(s, &off1, STR_NO_LIMIT);
     546                c2 = str_decode(p, &off2, STR_NO_LIMIT);
     547               
     548                if (c2 == 0)
     549                        return true;
     550
     551                if (c1 != c2)
     552                        return false;
     553               
     554                if (c1 == 0)
     555                        break;
     556        }
     557
     558        return false;
    510559}
    511560
     
    10851134                c = (c >= 'a' ? c - 'a' + 10 : (c >= 'A' ? c - 'A' + 10 :
    10861135                    (c <= '9' ? c - '0' : 0xff)));
    1087                 if (c > base) {
     1136                if (c >= base) {
    10881137                        break;
    10891138                }
Note: See TracChangeset for help on using the changeset viewer.