Ejemplo n.º 1
0
inline void* realloc(void* ptr, size_t size) {
	void* ptr2 = __realloc(ptr, size, __malloc_pool);
	#if DEBUG
	freed(ptr);
	malloced(((void**)read_rbp())[1], size, ptr2);
	#endif /* DEBUG */
	return ptr2;
}
Ejemplo n.º 2
0
void *zrealloc(void *ptr, size_t size)
{
	size_t oldsize;
	void *newptr;

	if (ptr == NULL)
		return zmalloc(size);
	oldsize = __malloc_size(ptr);
	newptr = __realloc(ptr, size);
	if (!newptr)
		zmalloc_oom_handler(size);

	update_zmalloc_stat_free(oldsize);
	update_zmalloc_stat_alloc(__malloc_size(newptr));
	return newptr;
}
Ejemplo n.º 3
0
static void realloc_func() {
	size_t pool_size = 0x40000;
	size_t malloc_size;

	for(size_t i = 1; i < pool_size - 0x3e001; i++) {
		malloc_size = i;

		void* malloc_pool = malloc(pool_size);
		init_memory_pool(pool_size, malloc_pool, 0);

		void* mem = __malloc(malloc_size, malloc_pool);		
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		mem = __realloc(mem, 0x3e000, malloc_pool);
		assert_in_range(mem, malloc_pool, malloc_pool + pool_size);

		destroy_memory_pool(malloc_pool);
		free(malloc_pool);
		malloc_pool = NULL;
	}
}
Ejemplo n.º 4
0
int __fdtable_entry_add(__fdtable_t * fdtable, int fileno, __fildes_t * fildes, __fildes_t ** newfd)
{
 int nFileNo;

 /* descriptors count reached OPEN_MAX */
 if(fdtable->UsedDescriptors >= OPEN_MAX)
 {
  ERR("file descriptor table full");
  errno = EMFILE;
  return (-1);
 }

 /* base fileno less than zero: use the lowest unused fileno */
 if(fileno < 0)
  nFileNo = fdtable->LowestUnusedFileNo;
 /* base fileno greater than or equal to zero: use the next available fileno */
 else
  nFileNo = __fdtable_entry_nextavail(fdtable, fileno);

 INFO("lowest unused file number is %d", nFileNo);

 /* descriptors count reached OPEN_MAX */
 if(nFileNo < 0)
 {
  ERR("nFileNo is less than zero");
  errno = EMFILE;
  return (-1);
 }

 /* if the table doesn't have enough space for the next entry ... */
 if(nFileNo >= fdtable->AllocatedDescriptors)
 {
  void * pTemp;

  INFO
  (
   "growing the array from %lu to %lu bytes",
   fdtable->AllocatedDescriptors * sizeof(*fdtable->Descriptors),
   (nFileNo + 1) * sizeof(*fdtable->Descriptors)
  );

  /* ... try to increase the size of the table */
  if(fdtable->AllocatedDescriptors * sizeof(*fdtable->Descriptors) == 0)
   pTemp = __malloc((nFileNo + 1) * sizeof(*fdtable->Descriptors));
  else
   pTemp = __realloc
   (
    fdtable->Descriptors,
    (nFileNo + 1) * sizeof(*fdtable->Descriptors)
   );

  /* reallocation failed */
  if(pTemp == 0)
  {
   ERR("__realloc() failed");
   errno = ENOMEM;
   return (-1);
  }

  /* update the table */
  fdtable->AllocatedDescriptors = nFileNo + 1;
  fdtable->Descriptors = pTemp;
 }

 /* initialize descriptor */
 if(fildes == 0)
  memset(&fdtable->Descriptors[nFileNo], 0, sizeof(__fildes_t));
 else
  memcpy(&fdtable->Descriptors[nFileNo], fildes, sizeof(__fildes_t));

 if(newfd != 0)
  *newfd = &fdtable->Descriptors[nFileNo];

 INFO
 (
  "file number %d: handle 0x%08X, open flags 0x%08X, flags 0x%08X, extra data size %u, extra data at 0x%08X",
  nFileNo,
  fdtable->Descriptors[nFileNo].FileHandle,
  fdtable->Descriptors[nFileNo].OpenFlags,
  fdtable->Descriptors[nFileNo].FdFlags,
  fdtable->Descriptors[nFileNo].ExtraDataSize,
  fdtable->Descriptors[nFileNo].ExtraData
 );

 INFO
 (
  "incrementing used descriptors count from %u to %u",
  fdtable->UsedDescriptors,
  fdtable->UsedDescriptors + 1
 );
 fdtable->UsedDescriptors ++;

 INFO
 (
  "setting bit %u of cell %u of the bitmap to 1",
  nFileNo % 32,
  nFileNo / 32
 );
 fdtable->DescriptorsBitmap[nFileNo / 32] |= (1 << (nFileNo % 32));

 fdtable->LowestUnusedFileNo = __fdtable_entry_nextavail(fdtable, nFileNo);
 INFO("setting the lowest unused file number to %d", fdtable->LowestUnusedFileNo);

 return (nFileNo);
}