mirror of
https://git.code.sf.net/p/quake/quakeforge
synced 2024-11-10 15:22:04 +00:00
Preliminary Qalloc implimentation. Not yet used.
This commit is contained in:
parent
5902dcca6a
commit
f423b8a8e0
2 changed files with 84 additions and 0 deletions
|
@ -137,4 +137,22 @@ void *Cache_Alloc (cache_user_t *c, int size, const char *name);
|
|||
|
||||
void Cache_Report (void);
|
||||
|
||||
/* Modes, pick one */
|
||||
#define QA_NOFAIL 1
|
||||
#define QA_LATEFAIL 2
|
||||
#define QA_EARLYFAIL 3
|
||||
|
||||
#define _QA_MODEMASK 3
|
||||
|
||||
/* Flags, OR them with the mode */
|
||||
#define QA_ZEROED 4
|
||||
|
||||
extern size_t (*Qalloc_callback) (size_t size);
|
||||
|
||||
void *Qalloc (void *ptr, size_t size, unsigned modes);
|
||||
void *Qmalloc (size_t size);
|
||||
void *Qcalloc (size_t nmemb, size_t size);
|
||||
void *Qrealloc (void *ptr, size_t size);
|
||||
void Qfree (void *ptr);
|
||||
|
||||
#endif // __zone_h
|
||||
|
|
|
@ -898,6 +898,72 @@ Cache_Profile (void)
|
|||
" %d per allocation\n", total, count, total / count);
|
||||
}
|
||||
|
||||
/*
|
||||
Qalloc and friends
|
||||
*/
|
||||
|
||||
size_t (*Qalloc_callback) (size_t size);
|
||||
|
||||
void *
|
||||
Qalloc (void *ptr, size_t size, unsigned modes)
|
||||
{
|
||||
void *mem;
|
||||
|
||||
if (modes & ~(_QA_MODEMASK | QA_ZEROED))
|
||||
Sys_Error ("Qalloc: bad modes field: %u\n", modes);
|
||||
|
||||
if (size) {
|
||||
do {
|
||||
if (ptr) {
|
||||
if (modes & QA_ZEROED)
|
||||
Sys_Error ("Qalloc: Zeroing reallocated memory not yet supported\n");
|
||||
else
|
||||
mem = realloc (ptr, size);
|
||||
} else {
|
||||
if (modes & QA_ZEROED)
|
||||
mem = calloc (size, 1);
|
||||
else
|
||||
mem = malloc (size);
|
||||
}
|
||||
} while ((modes & _QA_MODEMASK) != QA_EARLYFAIL && !mem && Qalloc_callback (size));
|
||||
|
||||
if (!mem && (modes & _QA_MODEMASK) == QA_NOFAIL)
|
||||
Sys_Error ("Qalloc: could not allocate %d bytes!\n", size);
|
||||
|
||||
return mem;
|
||||
} else {
|
||||
if (!ptr)
|
||||
Sys_Error ("Qalloc: can't free a NULL pointers!\n");
|
||||
free (ptr);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void *
|
||||
Qmalloc (size_t size)
|
||||
{
|
||||
return Qalloc (0, size, QA_NOFAIL);
|
||||
}
|
||||
|
||||
void *
|
||||
Qcalloc (size_t nmemb, size_t size)
|
||||
{
|
||||
return Qalloc (0, nmemb * size, QA_NOFAIL | QA_ZEROED);
|
||||
}
|
||||
|
||||
void *
|
||||
Qrealloc (void *ptr, size_t size)
|
||||
{
|
||||
return Qalloc (ptr, size, QA_NOFAIL);
|
||||
}
|
||||
|
||||
void
|
||||
Qfree (void *ptr)
|
||||
{
|
||||
Qalloc (ptr, 0, QA_NOFAIL);
|
||||
}
|
||||
|
||||
|
||||
//============================================================================
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in a new issue