Exemplo n.º 1
0
/*
 * realloc - Change the size of the block by mallocing a new block,
 *      copying its data, and freeing the old block.  I'm too lazy
 *      to do better.
 */
void *realloc(void *oldptr, size_t size)
{
  size_t oldsize;
  void *newptr;

  /* If size == 0 then this is just free, and we return NULL. */
  if(size == 0) {
    free(oldptr);
    return 0;
  }

  /* If oldptr is NULL, then this is just malloc. */
  if(oldptr == NULL) {
    return malloc(size);
  }

  newptr = malloc(size);

  /* If realloc() fails the original block is left untouched  */
  if(!newptr) {
    return 0;
  }

  /* Copy the old data. */
  oldsize = *SIZE_PTR(oldptr);
  if(size < oldsize) oldsize = size;
  memcpy(newptr, oldptr, oldsize);

  /* Free the old block. */
  free(oldptr);

  return newptr;
}
Exemplo n.º 2
0
/*
 * malloc - Allocate a block by incrementing the brk pointer.
 *			Always allocate a block whose size is a multiple of the alignment.
 */
void *malloc(size_t size){
	int newsize = ALIGN(size + SIZE_T_SIZE);
	unsigned char *p = mem_sbrk(newsize);
	//dbg_printf("malloc %u => %p\n", size, p);

	if ((long)p < 0)
		return NULL;
	else {
		p += SIZE_T_SIZE;
		*SIZE_PTR(p) = size;
		return p;
	}
}
Exemplo n.º 3
0
/*
 * malloc
 */
void *malloc(size_t size)
{
	int newsize = ALIGN(size + SIZE_T_SIZE);
	unsigned char *p = mem_sbrk(newsize);
	
	if( (long)p < 0 )
		return NULL;
	else
	{
		p += SIZE_T_SIZE;
		*SIZE_PTR(p) = newsize;
		
		return p;
	}
}