Correct options when building for RetroArch.
[SquirrelJME.git] / ratufacoat / src / sjmercnm.c
blobb4a7eed78ec3265772f39e87c3610d38f4a2cf62
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // --------------------------------------------------------------------------*/
10 /**
11 * Native memory functions.
13 * @since 2019/06/25
16 /* Palm OS Functions. */
17 #if defined(__palmos__)
18 #include <MemoryMgr.h>
19 #include <MemGlue.h>
20 #endif
22 #include "sjmerc.h"
24 /** Allocates the given number of bytes. */
25 void* sjme_malloc(sjme_jint size)
27 void* rv;
29 /* These will never allocate. */
30 if (size <= 0)
31 return NULL;
33 /* Round size and include extra 4-bytes for size storage. */
34 size = ((size + SJME_JINT_C(3)) & (~SJME_JINT_C(3))) + SJME_JINT_C(4);
36 /* Exceeds maximum permitted allocation size? */
37 if (sizeof(sjme_jint) > sizeof(size_t) && size > (sjme_jint)SIZE_MAX)
38 return NULL;
40 #if defined(__palmos__)
41 /* Palm OS, use glue to allow greater than 64K. */
42 rv = MemGluePtrNew(size);
43 #else
44 /* Use standard C function otherwise. */
45 rv = calloc(1, size);
46 #endif
48 /* Did not allocate? */
49 if (rv == NULL)
50 return NULL;
52 #if defined(__palmos__)
53 /* Clear memory on Palm OS. */
54 MemSet(rv, size, 0);
55 #endif
57 /* Store the size into this memory block for later free. */
58 *((sjme_jint*)rv) = size;
60 /* Return the adjusted pointer. */
61 return SJME_POINTER_OFFSET_LONG(rv, 4);
64 /** Frees the given pointer. */
65 void sjme_free(void* p)
67 void* basep;
69 /* Ignore null pointers. */
70 if (p == NULL)
71 return;
73 /* Base pointer which is size shifted. */
74 basep = SJME_POINTER_OFFSET_LONG(p, -4);
76 #if defined(__palmos__)
77 /* Use Palm OS free. */
78 MemPtrFree(basep);
79 #else
80 /* Use Standard C free. */
81 free(basep);
82 #endif