lbnmem.h 1.9 KB

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