blob: f77298b3272ca6d79a1d272ec69cabc208616527 [file] [log] [blame]
Alexandre Lision7fd5d3d2013-12-04 13:06:40 -05001/*
2 * Operations on the usual buffers of bytes
3 */
4#ifndef BNSECURE
5#define BNSECURE 1
6#endif
7
8/*
9 * These operations act on buffers of memory, just like malloc & free.
10 * One exception: it is not legal to pass a NULL pointer to lbnMemFree.
11 */
12
13#ifndef lbnMemAlloc
14void *lbnMemAlloc(unsigned bytes);
15#endif
16
17#ifndef lbnMemFree
18void lbnMemFree(void *ptr, unsigned bytes);
19#endif
20
21/* This wipes out a buffer of bytes if necessary needed. */
22
23#ifndef lbnMemWipe
24#if BNSECURE
25void lbnMemWipe(void *ptr, unsigned bytes);
26#else
27#define lbnMemWipe(ptr, bytes) (void)(ptr,bytes)
28#endif
29#endif /* !lbnMemWipe */
30
31/*
32 * lbnRealloc is NOT like realloc(); it's endian-sensitive!
33 * If lbnMemRealloc is #defined, lbnRealloc will be defined in terms of it.
34 * It is legal to pass a NULL pointer to lbnRealloc, although oldbytes
35 * will always be sero.
36 */
37#ifndef lbnRealloc
38void *lbnRealloc(void *ptr, unsigned oldbytes, unsigned newbytes);
39#endif
40
41
42/*
43 * These macros are the ones actually used most often in the math library.
44 * They take and return pointers to the *end* of the given buffer, and
45 * take sizes in terms of words, not bytes.
46 *
47 * Note that LBNALLOC takes the pointer as an argument instead of returning
48 * the value.
49 *
50 * Note also that these macros are only useable if you have included
51 * lbn.h (for the BIG and BIGLITTLE macros), which this file does NOT include.
52 */
53
54#define LBNALLOC(p,type,words) BIGLITTLE( \
55 if ( ((p) = (type *)lbnMemAlloc((words)*sizeof*(p))) != 0) \
56 (p) += (words), \
57 (p) = (type *)lbnMemAlloc((words) * sizeof*(p)) \
58 )
59#define LBNFREE(p,words) lbnMemFree((p) BIG(-(words)), (words) * sizeof*(p))
60#define LBNREALLOC(p,old,new) \
61 lbnRealloc(p, (old) * sizeof*(p), (new) * sizeof*(p))
62#define LBNWIPE(p,words) lbnMemWipe((p) BIG(-(words)), (words) * sizeof*(p))
63