/* * place - Place block of asize bytes at start of free block bp * and split if remainder would be at least minimum block size */ static void place(block_ptr bp, size_t asize) { size_t csize = GET_SIZE(HDRP(bp)), delta = csize - asize; if (delta >= (2 * DSIZE)) { PUT(HDRP(bp), PACK(asize, 1)); PUT(FTRP(bp), PACK(asize, 1)); SET_ALLOC(bp); bp = NEXT_BLKP(bp); PUT(HDRP(bp), PACK(delta, 0)); PUT(FTRP(bp), PACK(delta, 0)); SET_UNALLOC(bp); reset_block(bp); insert_free_block(bp, delta); #ifdef DEBUG { printf("Block with size %zu remains a block:\n", asize); printblock(bp); } #endif } else { PUT(HDRP(bp), PACK(csize, 1)); PUT(FTRP(bp), PACK(csize, 1)); SET_ALLOC(bp); } }
/* init a new mp_int */ int mp_init (mp_int * a) { #ifdef __UHC_BUILDS_RTS__ // all allocation is assumed to be done outside library mp_zero(a) ; printf( "WARNING: mp_init (%p used=%x alc=%x)\n", a, USED(a), ALLOC(a) ) ; // prLTM(a,"mp_init") ; #else int i; /* allocate memory required and clear it */ SET_DIGITS(a, OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC)); if (DIGITS(a) == NULL) { return MP_MEM; } /* set the digits to zero */ for (i = 0; i < MP_PREC; i++) { SET_DIGIT(a,i,0); } /* set the used to zero, allocated digits to the default precision * and sign to positive */ SET_USED(a,0); SET_ALLOC(a,MP_PREC); SET_SIGN(a,MP_ZPOS); #endif return MP_OKAY; }
/* * Initialize: return -1 on error, 0 on success. */ int mm_init(void) { /* Create the initial empty heap */ if ((bins = mem_sbrk( ALIGN(fixed_bin_count * sizeof(block_ptr)) + 4 * WSIZE)) == (block_ptr)-1) return -1; memset(bins, 0, fixed_bin_count * sizeof(block_ptr)); heap_listp = (char *)ALIGN((unsigned long)(bins + fixed_bin_count)); larger_bin_root = NULL; PUTTRUNC(heap_listp, 0); /* Alignment padding */ PUTTRUNC(heap_listp + (1 * WSIZE), PACK(DSIZE, 1)); /* Prologue header */ PUTTRUNC(heap_listp + (2 * WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ PUTTRUNC(heap_listp + (3 * WSIZE), PACK(0, 1)); /* Epilogue header */ heap_listp += (2 * WSIZE); SET_ALLOC(heap_listp); #ifdef DEBUG { printblock(heap_listp); checkblock(heap_listp); } operid = 0; #endif return 0; }
/// <summary> /// Does what you'd expect the malloc C standard library to do, check /// the requirements document for more details. /// <summary> /// <param name='size'> How many bytes the user needs to allocate </param> /// <return> /// A properely aligned pointer to a block of memory whose size at /// is at least equal to the size requested by the caller. /// </return> void *my_malloc(size_t size) { uint8_t *user_data; /* The pointer we will return to the user */ uint8_t *new_block; /* Used to point to the block added by grow_heap */ uint8_t *sliced_block; /* Used to point to the left-over of a block */ struct block_header *header, *footer, *slice_header; int slice_list;/* Which list the slice belongs to */ if(size <= 0) return NULL; size += sizeof(struct block_header) * 2; /* The header+footer */ size = (size+7) & ~7;/* Align the size to 8-byte boundary */ /* Identify which list to pick from */ int list_num = pick_list(size); DEBUG_PRINT("size requested: %zd\n", size); DEBUG_PRINT("List picked: %d\n", list_num); user_data = extract_free_block(list_num, size); /* If no list had enough space */ if(user_data == NULL) { if((new_block = grow_heap(size)) == NULL) { DEBUG_PRINT("%s\n", "-------------------------------------"); errno = ENOMEM; return NULL; } add_free_block_to_list(list_num, new_block); user_data = extract_free_block(list_num, size); } assert(user_data != NULL); DEBUG_PRINT("%s\n", "Found a block!"); /* If we can slice, add the left-over back to our lists */ sliced_block = slice_block(user_data, size); if(sliced_block != NULL) { slice_header = (struct block_header *) sliced_block; slice_list = pick_list(slice_header->block_size); add_free_block_to_list(slice_list, sliced_block); } /* Mark the block as allocated */ header = (struct block_header *) user_data; SET_ALLOC(header); footer = get_footer(user_data); SET_ALLOC(footer); user_data = user_data + 8;/* Now points past the header */ DEBUG_PRINT("%s\n", "-------------------------------------"); return user_data; }
/* place - * Places the requested block at the beginning of the freeblock, and splitting * only if the size of the remainder block would equal or exceed the minimum * block size */ static void place(void *bp, size_t asize) { size_t csize = GET_SIZE(HDRP(bp)); size_t remainder = csize - asize; if (remainder >= (MINSIZE * WSIZE)) { remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp); SET_SIZE(HDRP(bp), asize); SET_ALLOC(HDRP(bp)); bp = NEXT_BLKP(bp); SET_SIZE(HDRP(bp), remainder); SET_SIZE(FTRP(bp), remainder); UNSET_ALLOC(HDRP(bp)); UNSET_ALLOC(FTRP(bp)); SET_TAG(HDRP(bp)); SET_TAG(FTRP(bp)); add_to_list(find_list(GET_SIZE(HDRP(bp))), bp); }else { remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp); SET_ALLOC(HDRP(bp)); SET_TAG(HDRP(NEXT_BLKP(bp))); } }