luat_bget.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Interface definitions for luat_bget.c, the memory management package.
  3. 在原版基础上,改成纯函数实现
  4. */
  5. //#include <assert.h>
  6. #include <string.h>
  7. typedef long bufsize;
  8. #define MemSize int /* Type for size arguments to memxxx()
  9. functions such as memcmp(). */
  10. #define SizeQuant 8 /* Buffer allocation size quantum:
  11. all buffers allocated are a
  12. multiple of this size. This
  13. MUST be a power of two. */
  14. #if 1
  15. #define BufStats 1 /* Define this symbol to enable the
  16. bstats() function which calculates
  17. the total free space in the buffer
  18. pool, the largest available
  19. buffer, and the total space
  20. currently allocated. */
  21. #endif
  22. #if 0
  23. #define FreeWipe 1 /* Wipe free buffers to a guaranteed
  24. pattern of garbage to trip up
  25. miscreants who attempt to use
  26. pointers into released buffers. */
  27. #endif
  28. #if 1
  29. #define BestFit 1 /* Use a best fit algorithm when
  30. searching for space for an
  31. allocation request. This uses
  32. memory more efficiently, but
  33. allocation will be much slower. */
  34. #endif
  35. /* Queue links */
  36. struct qlinks {
  37. struct bfhead *flink; /* Forward link */
  38. struct bfhead *blink; /* Backward link */
  39. };
  40. /* Header in allocated and free buffers */
  41. struct bhead {
  42. bufsize prevfree; /* Relative link back to previous
  43. free buffer in memory or 0 if
  44. previous buffer is allocated. */
  45. bufsize bsize; /* Buffer size: positive if free,
  46. negative if allocated. */
  47. };
  48. #define BH(p) ((struct bhead *) (p))
  49. /* Header in directly allocated buffers (by acqfcn) */
  50. struct bdhead {
  51. bufsize tsize; /* Total size, including overhead */
  52. struct bhead bh; /* Common header */
  53. };
  54. #define BDH(p) ((struct bdhead *) (p))
  55. /* Header in free buffers */
  56. struct bfhead {
  57. struct bhead bh; /* Common allocated/free header */
  58. struct qlinks ql; /* Links on free list */
  59. };
  60. #define BFH(p) ((struct bfhead *) (p))
  61. // static struct bfhead freelist = { /* List of free buffers */
  62. // {0, 0},
  63. // {&freelist, &freelist}
  64. // };
  65. typedef struct luat_bget
  66. {
  67. #ifdef BufStats
  68. bufsize totalloc; /* Total space currently allocated */
  69. bufsize maxalloc;
  70. unsigned long numget;
  71. unsigned long numrel; /* Number of bget() and brel() calls */
  72. #endif /* BufStats */
  73. struct bfhead freelist;
  74. }luat_bget_t;
  75. void luat_bget_init(luat_bget_t* bg);
  76. void luat_bpool(luat_bget_t* bg, void *buffer, bufsize len);
  77. void *luat_bget(luat_bget_t* bg, bufsize size);
  78. void *luat_bgetz(luat_bget_t* bg, bufsize size);
  79. void *luat_bgetr(luat_bget_t* bg, void *buffer, bufsize newsize);
  80. void luat_brel(luat_bget_t* bg, void *buf);
  81. // void luat_bectl(luat_bget_t* bg, int (*compact)(bufsize sizereq, int sequence), void *(*acquire)(bufsize size), void (*release)(void *buf), bufsize pool_incr);
  82. void luat_bstats(luat_bget_t* bg, bufsize *curalloc, bufsize *totfree, bufsize *maxfree, unsigned long *nget, unsigned long *nrel);
  83. // void luat_bstatse(luat_bget_t* bg, bufsize *pool_incr, long *npool, unsigned long *npget, unsigned long *nprel, unsigned long *ndget, unsigned long *ndrel);
  84. // void luat_bufdump(luat_bget_t* bg, void *buf);
  85. // void luat_bpoold(luat_bget_t* bg, void *pool, int dumpalloc, int dumpfree);
  86. // int luat_bpoolv(luat_bget_t* bg, void *pool);
  87. bufsize luat_bstatsmaxget(luat_bget_t* bg);