lbnmem.h 1.8 KB

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