Browse Source

Changed heap_extend so that it only requests just enough for the last block.

This behavior is better for realloc, but also for malloc.
main
Titouan Rigoudy 11 years ago
parent
commit
c4643161ff
4 changed files with 32 additions and 2 deletions
  1. +14
    -1
      ya_block.c
  2. +2
    -1
      ya_block.h
  3. +10
    -0
      ya_freelist.c
  4. +6
    -0
      ya_freelist.h

+ 14
- 1
ya_block.c View File

@ -53,6 +53,10 @@ static inline intptr_t round_to(intptr_t n, intptr_t m) {
return round_div(n,m) * m; return round_div(n,m) * m;
} }
static inline intptr_t inner_bytes(intptr_t *block) {
return (block_size(block) - 4) * WORD_SIZE;
}
/*----------------------*/ /*----------------------*/
/* Function definitions */ /* Function definitions */
/*----------------------*/ /*----------------------*/
@ -201,9 +205,18 @@ intptr_t *heap_init() {
return heap_start; return heap_start;
} }
/* Extends the heap by at least n_bytes bytes by calling sbrk.
/* Extends the heap enough for the last block to be at least n_bytes bytes
* large by calling sbrk.
* Returns a pointer to the last (free) block or NULL in case of failure. */ * Returns a pointer to the last (free) block or NULL in case of failure. */
intptr_t *heap_extend(size_t n_bytes) { intptr_t *heap_extend(size_t n_bytes) {
intptr_t *last = fl_get_end();
if (last) {
intptr_t last_bytes = inner_bytes(last);
if (last_bytes >= n_bytes) {
return last;
}
n_bytes -= last_bytes;
}
// request an integer number of blocks // request an integer number of blocks
intptr_t size = block_fit(round_to(n_bytes, CHUNK_SIZE)); intptr_t size = block_fit(round_to(n_bytes, CHUNK_SIZE));
void *ptr = sbrk(WORD_SIZE * size); void *ptr = sbrk(WORD_SIZE * size);


+ 2
- 1
ya_block.h View File

@ -63,7 +63,8 @@ static inline intptr_t block_size(intptr_t *block) {
* Returns the pointer to the start of the heap or NULL in case of failure. */ * Returns the pointer to the start of the heap or NULL in case of failure. */
intptr_t *heap_init(); intptr_t *heap_init();
/* Extends the heap by at least n_bytes bytes by calling sbrk.
/* Extends the heap enough for the last block to be at least n_bytes bytes
* large by calling sbrk.
* Returns a pointer to the last (free) block or NULL in case of failure. */ * Returns a pointer to the last (free) block or NULL in case of failure. */
intptr_t *heap_extend(size_t n_bytes); intptr_t *heap_extend(size_t n_bytes);


+ 10
- 0
ya_freelist.c View File

@ -147,3 +147,13 @@ void fl_join(intptr_t *block) {
fl_join_next(block); fl_join_next(block);
fl_join_prev(block); fl_join_prev(block);
} }
/* Returns a pointer to the first free block. */
intptr_t *fl_get_start() {
return fl_start;
}
/* Returns a pointer to the last free block. */
intptr_t *fl_get_end() {
return fl_end;
}

+ 6
- 0
ya_freelist.h View File

@ -71,4 +71,10 @@ void fl_join_next(intptr_t *block);
void fl_join(intptr_t *block); void fl_join(intptr_t *block);
/* Returns a pointer to the first free block. */
intptr_t *fl_get_start();
/* Returns a pointer to the last free block. */
intptr_t *fl_get_end();
#endif #endif

Loading…
Cancel
Save