[gamecode] Conform with libc malloc for pr zone

Allocating 0 bytes returns null and freeing a null pointer is safe.
This commit is contained in:
Bill Currie 2021-09-26 12:03:44 +09:00
parent ac25d9f1c1
commit 349f8067c3

View file

@ -64,22 +64,32 @@ PR_Zone_Init (progs_t *pr)
VISIBLE void VISIBLE void
PR_Zone_Free (progs_t *pr, void *ptr) PR_Zone_Free (progs_t *pr, void *ptr)
{ {
if (ptr) {
Z_Free (pr->zone, ptr); Z_Free (pr->zone, ptr);
} }
}
VISIBLE void * VISIBLE void *
PR_Zone_Malloc (progs_t *pr, pr_int_t size) PR_Zone_Malloc (progs_t *pr, pr_int_t size)
{ {
if (size <= 0) if (!size) {
PR_RunError (pr, "attempt to allocate less than 1 byte"); return 0;
}
if (size < 0) {
PR_RunError (pr, "attempt to allocate less than 0 bytes");
}
return Z_Malloc (pr->zone, size); return Z_Malloc (pr->zone, size);
} }
VISIBLE void * VISIBLE void *
PR_Zone_TagMalloc (progs_t *pr, int size, int tag) PR_Zone_TagMalloc (progs_t *pr, int size, int tag)
{ {
if (size <= 0) if (!size) {
PR_RunError (pr, "attempt to allocate less than 1 byte"); return 0;
}
if (size < 0) {
PR_RunError (pr, "attempt to allocate less than 0 bytes");
}
return Z_TagMalloc (pr->zone, size, tag); return Z_TagMalloc (pr->zone, size, tag);
} }
@ -90,7 +100,11 @@ PR_Zone_Realloc (progs_t *pr, void *ptr, pr_int_t size)
Z_Free (pr->zone, ptr); Z_Free (pr->zone, ptr);
return 0; return 0;
} }
if (size <= 0) if (!size) {
PR_RunError (pr, "attempt to allocate less than 1 byte"); return 0;
}
if (size < 0) {
PR_RunError (pr, "attempt to allocate less than 0 bytes");
}
return Z_Realloc (pr->zone, ptr, size); return Z_Realloc (pr->zone, ptr, size);
} }