Changeset 013a5d7 in mainline


Ignore:
Timestamp:
2011-05-17T15:13:00Z (13 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
38c773e7
Parents:
f414851
Message:

add proper heap shrinking implementation
add malloc3 test (a derivative of malloc1 which forces the allocator to create multiple heap areas)

Location:
uspace
Files:
4 added
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/Makefile

    rf414851 r013a5d7  
    4848        ipc/ping_pong.c \
    4949        loop/loop1.c \
     50        mm/common.c \
    5051        mm/malloc1.c \
    5152        mm/malloc2.c \
     53        mm/malloc3.c \
    5254        devs/devman1.c \
    5355        hw/misc/virtchar1.c \
  • uspace/app/tester/mm/malloc1.c

    rf414851 r013a5d7  
    3030
    3131#include <stdio.h>
    32 #include <unistd.h>
    3332#include <stdlib.h>
    34 #include <malloc.h>
     33#include "common.h"
    3534#include "../tester.h"
    3635
     
    4544 */
    4645
    47 /**
    48  * sizeof_array
    49  * @array array to determine the size of
    50  *
    51  * Returns the size of @array in array elements.
    52  */
    53 #define sizeof_array(array) \
    54         (sizeof(array) / sizeof((array)[0]))
    55 
    56 #define MAX_ALLOC (16 * 1024 * 1024)
    57 
    58 /*
    59  * Subphase control structures: subphase termination conditions,
    60  * probabilities of individual actions, subphase control structure.
    61  */
    62 
    63 typedef struct {
    64         unsigned int max_cycles;
    65         unsigned int no_memory;
    66         unsigned int no_allocated;
    67 } sp_term_cond_s;
    68 
    69 typedef struct {
    70         unsigned int alloc;
    71         unsigned int free;
    72 } sp_action_prob_s;
    73 
    74 typedef struct {
    75         const char *name;
    76         sp_term_cond_s cond;
    77         sp_action_prob_s prob;
    78 } subphase_s;
    79 
    80 
    81 /*
    82  * Phase control structures: The minimum and maximum block size that
    83  * can be allocated during the phase execution, phase control structure.
    84  */
    85 
    86 typedef struct {
    87         size_t min_block_size;
    88         size_t max_block_size;
    89 } ph_alloc_size_s;
    90 
    91 typedef struct {
    92         const char *name;
    93         ph_alloc_size_s alloc;
    94         subphase_s *subphases;
    95 } phase_s;
    96 
    97 
    9846/*
    9947 * Subphases are defined separately here. This is for two reasons:
     
    10149 * how many subphases a phase contains.
    10250 */
    103 static subphase_s subphases_32B[] = {
     51static subphase_t subphases_32B[] = {
    10452        {
    10553                .name = "Allocation",
     
    14088};
    14189
    142 static subphase_s subphases_128K[] = {
     90static subphase_t subphases_128K[] = {
    14391        {
    14492                .name = "Allocation",
     
    179127};
    180128
    181 static subphase_s subphases_default[] = {
     129static subphase_t subphases_default[] = {
    182130        {
    183131                .name = "Allocation",
     
    217165        }
    218166};
    219 
    220167
    221168/*
    222169 * Phase definitions.
    223170 */
    224 static phase_s phases[] = {
     171static phase_t phases[] = {
    225172        {
    226173                .name = "32 B memory blocks",
     
    257204};
    258205
    259 
    260 /*
    261  * Global error flag. The flag is set if an error
    262  * is encountered (overlapping blocks, inconsistent
    263  * block data, etc.)
    264  */
    265 static bool error_flag = false;
    266 
    267 /*
    268  * Memory accounting: the amount of allocated memory and the
    269  * number and list of allocated blocks.
    270  */
    271 static size_t mem_allocated;
    272 static size_t mem_blocks_count;
    273 
    274 static LIST_INITIALIZE(mem_blocks);
    275 
    276 typedef struct {
    277         /* Address of the start of the block */
    278         void *addr;
    279        
    280         /* Size of the memory block */
    281         size_t size;
    282        
    283         /* link to other blocks */
    284         link_t link;
    285 } mem_block_s;
    286 
    287 typedef mem_block_s *mem_block_t;
    288 
    289 
    290 /** init_mem
    291  *
    292  * Initializes the memory accounting structures.
    293  *
    294  */
    295 static void init_mem(void)
     206static void do_subphase(phase_t *phase, subphase_t *subphase)
    296207{
    297         mem_allocated = 0;
    298         mem_blocks_count = 0;
    299 }
    300 
    301 
    302 static bool overlap_match(link_t *entry, void *addr, size_t size)
    303 {
    304         mem_block_t mblk = list_get_instance(entry, mem_block_s, link);
    305        
    306         /* Entry block control structure <mbeg, mend) */
    307         uint8_t *mbeg = (uint8_t *) mblk;
    308         uint8_t *mend = (uint8_t *) mblk + sizeof(mem_block_s);
    309        
    310         /* Entry block memory <bbeg, bend) */
    311         uint8_t *bbeg = (uint8_t *) mblk->addr;
    312         uint8_t *bend = (uint8_t *) mblk->addr + mblk->size;
    313        
    314         /* Data block <dbeg, dend) */
    315         uint8_t *dbeg = (uint8_t *) addr;
    316         uint8_t *dend = (uint8_t *) addr + size;
    317        
    318         /* Check for overlaps */
    319         if (((mbeg >= dbeg) && (mbeg < dend)) ||
    320                 ((mend > dbeg) && (mend <= dend)) ||
    321                 ((bbeg >= dbeg) && (bbeg < dend)) ||
    322                 ((bend > dbeg) && (bend <= dend)))
    323                 return true;
    324        
    325         return false;
    326 }
    327 
    328 
    329 /** test_overlap
    330  *
    331  * Test whether a block starting at @addr overlaps with another, previously
    332  * allocated memory block or its control structure.
    333  *
    334  * @param addr Initial address of the block
    335  * @param size Size of the block
    336  *
    337  * @return false if the block does not overlap.
    338  *
    339  */
    340 static int test_overlap(void *addr, size_t size)
    341 {
    342         link_t *entry;
    343         bool fnd = false;
    344        
    345         for (entry = mem_blocks.next; entry != &mem_blocks; entry = entry->next) {
    346                 if (overlap_match(entry, addr, size)) {
    347                         fnd = true;
    348                         break;
    349                 }
    350         }
    351        
    352         return fnd;
    353 }
    354 
    355 
    356 /** checked_malloc
    357  *
    358  * Allocate @size bytes of memory and check whether the chunk comes
    359  * from the non-mapped memory region and whether the chunk overlaps
    360  * with other, previously allocated, chunks.
    361  *
    362  * @param size Amount of memory to allocate
    363  *
    364  * @return NULL if the allocation failed. Sets the global error_flag to
    365  *         true if the allocation succeeded but is illegal.
    366  *
    367  */
    368 static void *checked_malloc(size_t size)
    369 {
    370         void *data;
    371        
    372         /* Allocate the chunk of memory */
    373         data = malloc(size);
    374         if (data == NULL)
    375                 return NULL;
    376        
    377         /* Check for overlaps with other chunks */
    378         if (test_overlap(data, size)) {
    379                 TPRINTF("\nError: Allocated block overlaps with another "
    380                         "previously allocated block.\n");
    381                 error_flag = true;
    382         }
    383        
    384         return data;
    385 }
    386 
    387 
    388 /** alloc_block
    389  *
    390  * Allocate a block of memory of @size bytes and add record about it into
    391  * the mem_blocks list. Return a pointer to the block holder structure or
    392  * NULL if the allocation failed.
    393  *
    394  * If the allocation is illegal (e.g. the memory does not come from the
    395  * right region or some of the allocated blocks overlap with others),
    396  * set the global error_flag.
    397  *
    398  * @param size Size of the memory block
    399  *
    400  */
    401 static mem_block_t alloc_block(size_t size)
    402 {
    403         /* Check for allocation limit */
    404         if (mem_allocated >= MAX_ALLOC)
    405                 return NULL;
    406        
    407         /* Allocate the block holder */
    408         mem_block_t block = (mem_block_t) checked_malloc(sizeof(mem_block_s));
    409         if (block == NULL)
    410                 return NULL;
    411        
    412         link_initialize(&block->link);
    413        
    414         /* Allocate the block memory */
    415         block->addr = checked_malloc(size);
    416         if (block->addr == NULL) {
    417                 free(block);
    418                 return NULL;
    419         }
    420        
    421         block->size = size;
    422        
    423         /* Register the allocated block */
    424         list_append(&block->link, &mem_blocks);
    425         mem_allocated += size + sizeof(mem_block_s);
    426         mem_blocks_count++;
    427        
    428         return block;
    429 }
    430 
    431 
    432 /** free_block
    433  *
    434  * Free the block of memory and the block control structure allocated by
    435  * alloc_block. Set the global error_flag if an error occurs.
    436  *
    437  * @param block Block control structure
    438  *
    439  */
    440 static void free_block(mem_block_t block)
    441 {
    442         /* Unregister the block */
    443         list_remove(&block->link);
    444         mem_allocated -= block->size + sizeof(mem_block_s);
    445         mem_blocks_count--;
    446        
    447         /* Free the memory */
    448         free(block->addr);
    449         free(block);
    450 }
    451 
    452 
    453 /** expected_value
    454  *
    455  * Compute the expected value of a byte located at @pos in memory
    456  * block described by @blk.
    457  *
    458  * @param blk Memory block control structure
    459  * @param pos Position in the memory block data area
    460  *
    461  */
    462 static inline uint8_t expected_value(mem_block_t blk, uint8_t *pos)
    463 {
    464         return ((unsigned long) blk ^ (unsigned long) pos) & 0xff;
    465 }
    466 
    467 
    468 /** fill_block
    469  *
    470  * Fill the memory block controlled by @blk with data.
    471  *
    472  * @param blk Memory block control structure
    473  *
    474  */
    475 static void fill_block(mem_block_t blk)
    476 {
    477         uint8_t *pos;
    478         uint8_t *end;
    479        
    480         for (pos = blk->addr, end = pos + blk->size; pos < end; pos++)
    481                 *pos = expected_value(blk, pos);
    482 }
    483 
    484 
    485 /** check_block
    486  *
    487  * Check whether the block @blk contains the data it was filled with.
    488  * Set global error_flag if an error occurs.
    489  *
    490  * @param blk Memory block control structure
    491  *
    492  */
    493 static void check_block(mem_block_t blk)
    494 {
    495         uint8_t *pos;
    496         uint8_t *end;
    497        
    498         for (pos = blk->addr, end = pos + blk->size; pos < end; pos++) {
    499                 if (*pos != expected_value (blk, pos)) {
    500                         TPRINTF("\nError: Corrupted content of a data block.\n");
    501                         error_flag = true;
    502                         return;
    503                 }
    504         }
    505 }
    506 
    507 
    508 static link_t *list_get_nth(link_t *list, unsigned int i)
    509 {
    510         unsigned int cnt = 0;
    511         link_t *entry;
    512        
    513         for (entry = list->next; entry != list; entry = entry->next) {
    514                 if (cnt == i)
    515                         return entry;
    516                
    517                 cnt++;
    518         }
    519        
    520         return NULL;
    521 }
    522 
    523 
    524 /** get_random_block
    525  *
    526  * Select a random memory block from the list of allocated blocks.
    527  *
    528  * @return Block control structure or NULL if the list is empty.
    529  *
    530  */
    531 static mem_block_t get_random_block(void)
    532 {
    533         if (mem_blocks_count == 0)
    534                 return NULL;
    535        
    536         unsigned int blkidx = rand() % mem_blocks_count;
    537         link_t *entry = list_get_nth(&mem_blocks, blkidx);
    538        
    539         if (entry == NULL) {
    540                 TPRINTF("\nError: Corrupted list of allocated memory blocks.\n");
    541                 error_flag = true;
    542         }
    543        
    544         return list_get_instance(entry, mem_block_s, link);
    545 }
    546 
    547 
    548 #define RETURN_IF_ERROR \
    549 { \
    550         if (error_flag) \
    551                 return; \
    552 }
    553 
    554 
    555 static void do_subphase(phase_s *phase, subphase_s *subphase)
    556 {
    557         unsigned int cycles;
    558         for (cycles = 0; /* always */; cycles++) {
    559                
    560                 if (subphase->cond.max_cycles &&
    561                         cycles >= subphase->cond.max_cycles) {
     208        for (unsigned int cycles = 0; /* always */; cycles++) {
     209               
     210                if ((subphase->cond.max_cycles) &&
     211                    (cycles >= subphase->cond.max_cycles)) {
    562212                        /*
    563213                         * We have performed the required number of
     
    572222                unsigned int rnd = rand() % 100;
    573223                if (rnd < subphase->prob.alloc) {
    574                         /* Compute a random number lying in interval <min_block_size, max_block_size> */
     224                        /*
     225                         * Compute a random number lying in interval
     226                         * <min_block_size, max_block_size>
     227                         */
    575228                        int alloc = phase->alloc.min_block_size +
    576229                            (rand() % (phase->alloc.max_block_size - phase->alloc.min_block_size + 1));
    577230                       
    578                         mem_block_t blk = alloc_block(alloc);
     231                        mem_block_t *blk = alloc_block(alloc);
    579232                        RETURN_IF_ERROR;
    580233                       
     
    585238                                        break;
    586239                                }
    587                                
    588240                        } else {
    589241                                TPRINTF("A");
     
    592244                       
    593245                } else if (rnd < subphase->prob.free) {
    594                         mem_block_t blk = get_random_block();
     246                        mem_block_t *blk = get_random_block();
    595247                        if (blk == NULL) {
    596248                                TPRINTF("F(R)");
     
    599251                                        break;
    600252                                }
    601                                
    602253                        } else {
    603254                                TPRINTF("R");
     
    614265}
    615266
    616 
    617 static void do_phase(phase_s *phase)
     267static void do_phase(phase_t *phase)
    618268{
    619         unsigned int subno;
    620        
    621         for (subno = 0; subno < 3; subno++) {
    622                 subphase_s *subphase = & phase->subphases [subno];
     269        for (unsigned int subno = 0; subno < 3; subno++) {
     270                subphase_t *subphase = &phase->subphases[subno];
    623271               
    624272                TPRINTF(".. Sub-phase %u (%s)\n", subno + 1, subphase->name);
     
    632280        init_mem();
    633281       
    634         unsigned int phaseno;
    635         for (phaseno = 0; phaseno < sizeof_array(phases); phaseno++) {
    636                 phase_s *phase = &phases[phaseno];
     282        for (unsigned int phaseno = 0; phaseno < sizeof_array(phases);
     283            phaseno++) {
     284                phase_t *phase = &phases[phaseno];
    637285               
    638286                TPRINTF("Entering phase %u (%s)\n", phaseno + 1, phase->name);
     
    645293        }
    646294       
     295        TPRINTF("Cleaning up.\n");
     296        done_mem();
    647297        if (error_flag)
    648298                return "Test failed";
  • uspace/app/tester/tester.c

    rf414851 r013a5d7  
    6363#include "mm/malloc1.def"
    6464#include "mm/malloc2.def"
     65#include "mm/malloc3.def"
    6566#include "hw/serial/serial1.def"
    6667#include "hw/misc/virtchar1.def"
  • uspace/app/tester/tester.h

    rf414851 r013a5d7  
    4646extern char **test_argv;
    4747
     48/**
     49 * sizeof_array
     50 * @array array to determine the size of
     51 *
     52 * Returns the size of @array in array elements.
     53 */
     54#define sizeof_array(array) \
     55        (sizeof(array) / sizeof((array)[0]))
     56
    4857#define TPRINTF(format, ...) \
    49         { \
     58        do { \
    5059                if (!test_quiet) { \
    51                         fprintf(stderr, format, ##__VA_ARGS__); \
     60                        fprintf(stderr, (format), ##__VA_ARGS__); \
    5261                } \
    53         }
     62        } while (0)
    5463
    5564typedef const char *(*test_entry_t)(void);
     
    7988extern const char *test_malloc1(void);
    8089extern const char *test_malloc2(void);
     90extern const char *test_malloc3(void);
    8191extern const char *test_serial1(void);
    8292extern const char *test_virtchar1(void);
  • uspace/lib/c/generic/malloc.c

    rf414851 r013a5d7  
    6565#define BASE_ALIGN  16
    6666
     67/** Heap shrink granularity
     68 *
     69 * Try not to pump and stress the heap to much
     70 * by shrinking and enlarging it too often.
     71 * A heap area won't shrunk if it the released
     72 * free block is smaller than this constant.
     73 *
     74 */
     75#define SHRINK_GRANULARITY  (64 * PAGE_SIZE)
     76
    6777/** Overhead of each heap block. */
    6878#define STRUCT_OVERHEAD \
     
    8696 *
    8797 */
    88 #define AREA_FIRST_BLOCK(area) \
     98#define AREA_FIRST_BLOCK_HEAD(area) \
    8999        (ALIGN_UP(((uintptr_t) (area)) + sizeof(heap_area_t), BASE_ALIGN))
     100
     101/** Get last block in heap area.
     102 *
     103 */
     104#define AREA_LAST_BLOCK_FOOT(area) \
     105        (((uintptr_t) (area)->end) - sizeof(heap_block_foot_t))
     106
     107/** Get header in heap block.
     108 *
     109 */
     110#define BLOCK_HEAD(foot) \
     111        ((heap_block_head_t *) \
     112            (((uintptr_t) (foot)) + sizeof(heap_block_foot_t) - (foot)->size))
    90113
    91114/** Get footer in heap block.
     
    94117#define BLOCK_FOOT(head) \
    95118        ((heap_block_foot_t *) \
    96             (((uintptr_t) head) + head->size - sizeof(heap_block_foot_t)))
     119            (((uintptr_t) (head)) + (head)->size - sizeof(heap_block_foot_t)))
    97120
    98121/** Heap area.
     
    115138        void *end;
    116139       
     140        /** Previous heap area */
     141        struct heap_area *prev;
     142       
    117143        /** Next heap area */
    118144        struct heap_area *next;
     
    212238/** Check a heap area structure
    213239 *
     240 * Should be called only inside the critical section.
     241 *
    214242 * @param addr Address of the heap area.
    215243 *
     
    220248       
    221249        assert(area->magic == HEAP_AREA_MAGIC);
     250        assert(addr == area->start);
    222251        assert(area->start < area->end);
    223252        assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
     
    227256/** Create new heap area
    228257 *
    229  * @param start Preffered starting address of the new area.
    230  * @param size  Size of the area.
     258 * Should be called only inside the critical section.
     259 *
     260 * @param size Size of the area.
    231261 *
    232262 */
     
    248278       
    249279        area->start = astart;
    250         area->end = (void *)
    251             ALIGN_DOWN((uintptr_t) astart + asize, BASE_ALIGN);
     280        area->end = (void *) ((uintptr_t) astart + asize);
     281        area->prev = NULL;
    252282        area->next = NULL;
    253283        area->magic = HEAP_AREA_MAGIC;
    254284       
    255         void *block = (void *) AREA_FIRST_BLOCK(area);
     285        void *block = (void *) AREA_FIRST_BLOCK_HEAD(area);
    256286        size_t bsize = (size_t) (area->end - block);
    257287       
     
    262292                last_heap_area = area;
    263293        } else {
     294                area->prev = last_heap_area;
    264295                last_heap_area->next = area;
    265296                last_heap_area = area;
     
    271302/** Try to enlarge a heap area
    272303 *
     304 * Should be called only inside the critical section.
     305 *
    273306 * @param area Heap area to grow.
    274  * @param size Gross size of item to allocate (bytes).
     307 * @param size Gross size to grow (bytes).
     308 *
     309 * @return True if successful.
    275310 *
    276311 */
     
    282317        area_check(area);
    283318       
    284         size_t asize = ALIGN_UP((size_t) (area->end - area->start) + size,
    285             PAGE_SIZE);
    286        
    287319        /* New heap area size */
    288         void *end = (void *)
    289             ALIGN_DOWN((uintptr_t) area->start + asize, BASE_ALIGN);
     320        size_t gross_size = (size_t) (area->end - area->start) + size;
     321        size_t asize = ALIGN_UP(gross_size, PAGE_SIZE);
     322        void *end = (void *) ((uintptr_t) area->start + asize);
    290323       
    291324        /* Check for overflow */
     
    299332       
    300333        /* Add new free block */
    301         block_init(area->end, (size_t) (end - area->end), true, area);
     334        size_t net_size = (size_t) (end - area->end);
     335        if (net_size > 0)
     336                block_init(area->end, net_size, true, area);
    302337       
    303338        /* Update heap area parameters */
     
    309344/** Try to enlarge any of the heap areas
    310345 *
     346 * Should be called only inside the critical section.
     347 *
    311348 * @param size Gross size of item to allocate (bytes).
    312349 *
     
    318355       
    319356        /* First try to enlarge some existing area */
    320         heap_area_t *area;
    321         for (area = first_heap_area; area != NULL; area = area->next) {
     357        for (heap_area_t *area = first_heap_area; area != NULL;
     358            area = area->next) {
    322359                if (area_grow(area, size))
    323360                        return true;
     
    325362       
    326363        /* Eventually try to create a new area */
    327         return area_create(AREA_FIRST_BLOCK(size));
    328 }
    329 
    330 /** Try to shrink heap space
    331  *
     364        return area_create(AREA_FIRST_BLOCK_HEAD(size));
     365}
     366
     367/** Try to shrink heap
     368 *
     369 * Should be called only inside the critical section.
    332370 * In all cases the next pointer is reset.
    333371 *
    334  */
    335 static void heap_shrink(void)
    336 {
     372 * @param area Last modified heap area.
     373 *
     374 */
     375static void heap_shrink(heap_area_t *area)
     376{
     377        area_check(area);
     378       
     379        heap_block_foot_t *last_foot =
     380            (heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area);
     381        heap_block_head_t *last_head = BLOCK_HEAD(last_foot);
     382       
     383        block_check((void *) last_head);
     384        assert(last_head->area == area);
     385       
     386        if (last_head->free) {
     387                /*
     388                 * The last block of the heap area is
     389                 * unused. The area might be potentially
     390                 * shrunk.
     391                 */
     392               
     393                heap_block_head_t *first_head =
     394                    (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(area);
     395               
     396                block_check((void *) first_head);
     397                assert(first_head->area == area);
     398               
     399                if (first_head == last_head) {
     400                        /*
     401                         * The entire heap area consists of a single
     402                         * free heap block. This means we can get rid
     403                         * of it entirely.
     404                         */
     405                       
     406                        heap_area_t *prev = area->prev;
     407                        heap_area_t *next = area->next;
     408                       
     409                        if (prev != NULL) {
     410                                area_check(prev);
     411                                prev->next = next;
     412                        } else
     413                                first_heap_area = next;
     414                       
     415                        if (next != NULL) {
     416                                area_check(next);
     417                                next->prev = prev;
     418                        } else
     419                                last_heap_area = prev;
     420                       
     421                        as_area_destroy(area->start);
     422                } else if (last_head->size >= SHRINK_GRANULARITY) {
     423                        /*
     424                         * Make sure that we always shrink the area
     425                         * by a multiple of page size and update
     426                         * the block layout accordingly.
     427                         */
     428                       
     429                        size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
     430                        size_t asize = (size_t) (area->end - area->start) - shrink_size;
     431                        void *end = (void *) ((uintptr_t) area->start + asize);
     432                       
     433                        /* Resize the address space area */
     434                        int ret = as_area_resize(area->start, asize, 0);
     435                        if (ret != EOK)
     436                                abort();
     437                       
     438                        /* Update heap area parameters */
     439                        area->end = end;
     440                       
     441                        /* Update block layout */
     442                        void *last = (void *) last_head;
     443                        size_t excess = (size_t) (area->end - last);
     444                       
     445                        if (excess > 0) {
     446                                if (excess >= STRUCT_OVERHEAD) {
     447                                        /*
     448                                         * The previous block cannot be free and there
     449                                         * is enough free space left in the area to
     450                                         * create a new free block.
     451                                         */
     452                                        block_init(last, excess, true, area);
     453                                } else {
     454                                        /*
     455                                         * The excess is small. Therefore just enlarge
     456                                         * the previous block.
     457                                         */
     458                                        heap_block_foot_t *prev_foot = (heap_block_foot_t *)
     459                                            (((uintptr_t) last_head) - sizeof(heap_block_foot_t));
     460                                        heap_block_head_t *prev_head = BLOCK_HEAD(prev_foot);
     461                                       
     462                                        block_check((void *) prev_head);
     463                                       
     464                                        block_init(prev_head, prev_head->size + excess,
     465                                            prev_head->free, area);
     466                                }
     467                        }
     468                       
     469                       
     470                }
     471        }
     472       
    337473        next = NULL;
    338474}
     
    398534{
    399535        area_check((void *) area);
    400         assert((void *) first_block >= (void *) AREA_FIRST_BLOCK(area));
     536        assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    401537        assert((void *) first_block < area->end);
    402538       
    403         heap_block_head_t *cur;
    404         for (cur = first_block; (void *) cur < area->end;
     539        for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
    405540            cur = (heap_block_head_t *) (((void *) cur) + cur->size)) {
    406541                block_check(cur);
     
    436571                                         * data in (including alignment).
    437572                                         */
    438                                         if ((void *) cur > (void *) AREA_FIRST_BLOCK(area)) {
     573                                        if ((void *) cur > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
    439574                                                /*
    440575                                                 * There is a block before the current block.
     
    496631                                                        size_t reduced_size = cur->size - excess;
    497632                                                        cur = (heap_block_head_t *)
    498                                                             (AREA_FIRST_BLOCK(area) + excess);
     633                                                            (AREA_FIRST_BLOCK_HEAD(area) + excess);
    499634                                                       
    500                                                         block_init((void *) AREA_FIRST_BLOCK(area), excess,
    501                                                             true, area);
     635                                                        block_init((void *) AREA_FIRST_BLOCK_HEAD(area),
     636                                                            excess, true, area);
    502637                                                        block_init(cur, reduced_size, true, area);
    503638                                                        split_mark(cur, real_size);
     
    552687       
    553688        /* Search the entire heap */
    554         heap_area_t *area;
    555         for (area = first_heap_area; area != NULL; area = area->next) {
     689        for (heap_area_t *area = first_heap_area; area != NULL;
     690            area = area->next) {
    556691                heap_block_head_t *first = (heap_block_head_t *)
    557                     AREA_FIRST_BLOCK(area);
     692                    AREA_FIRST_BLOCK_HEAD(area);
    558693               
    559694                void *addr = malloc_area(area, first, split, real_size,
     
    657792       
    658793        area_check(area);
    659         assert((void *) head >= (void *) AREA_FIRST_BLOCK(area));
     794        assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    660795        assert((void *) head < area->end);
    661796       
     
    675810                        block_init((void *) head + real_size,
    676811                            orig_size - real_size, true, area);
    677                         heap_shrink();
     812                        heap_shrink(area);
    678813                }
    679814               
     
    734869       
    735870        area_check(area);
    736         assert((void *) head >= (void *) AREA_FIRST_BLOCK(area));
     871        assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
    737872        assert((void *) head < area->end);
    738873       
     
    751886       
    752887        /* Look at the previous block. If it is free, merge the two. */
    753         if ((void *) head > (void *) AREA_FIRST_BLOCK(area)) {
     888        if ((void *) head > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
    754889                heap_block_foot_t *prev_foot =
    755890                    (heap_block_foot_t *) (((void *) head) - sizeof(heap_block_foot_t));
     
    765900        }
    766901       
    767         heap_shrink();
     902        heap_shrink(area);
    768903       
    769904        futex_up(&malloc_futex);
Note: See TracChangeset for help on using the changeset viewer.