README 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. ** Written by David Gerdes US Army Construction Engineering Research Lab
  3. ** April 1992
  4. ** Copyright 1992 USA-CERL All rights reserved.
  5. **
  6. */
  7. LINKED LIST MEMORY MANAGER
  8. Assumption: malloc/free is inefficient and wasteful for things like
  9. linked lists.
  10. Workaround: I found myself frequently writing a program specific memory
  11. manager to allocate large chunks which were then parcelled out.
  12. Solution: Develop a generic linked list memory manager which does all the
  13. work for you.
  14. Descriptn: Remember those school days when we used Pascal.
  15. Remember the new and dispose functions? Well that's what
  16. you get with this library. The front end consists of
  17. init(), new(), dispose(), and cleanup().
  18. The innards is a memory manager which provides a structure on
  19. demand and maintains its own efficient free memory list
  20. to minimize the number of calls to malloc() and free().
  21. Notes: The memory returned by link_new() is NOT guaranteed to be zeroed.
  22. This could be added as an option later if there is demand.
  23. The key item for this library is that the linked list is
  24. a list of homogenous elements all of the same size. what
  25. is returned by link_new() for a given token is always the
  26. same size.
  27. Unlike free(), the contents of the disposed structure are
  28. no longer available. The link_dispose () modifies part of
  29. the disposed of structure.
  30. The minimum size of a structure is the size of a pointer.
  31. If the structure size is less than that, it will be increased
  32. to that minimum.
  33. Interface:
  34. The link structure must have the following form:
  35. struct my_linked_list
  36. {
  37. struct my_linked_list *next;
  38. <data_type> data;
  39. /* optional other data */
  40. };
  41. The link to the next item MUST be the first entry.
  42. void *
  43. link_init (int size)
  44. initialize the system for a given linked list. Size is the size
  45. of your link structure.
  46. Returns a token which identifies that list's manager.
  47. You can have several different lists actively managed with
  48. a different structure for each.
  49. void
  50. link_set_chunk_size (int cnt)
  51. changes the number of structures requested in each malloc
  52. If this routine is not called, the default is 100.
  53. Calling this routine affects all further calls to link_init()
  54. or until the next call to link_set_chunk_size().
  55. void
  56. link_cleanup (struct link_head *token)
  57. Clean up all memory when done using the list.
  58. should only be called once per list per run for best performance.
  59. Pass it the token returned by link_init ()
  60. void *
  61. link_new (struct link_head *token)
  62. return a new memory slot, 'size' bytes long as specified by link_init()
  63. This return pointer should be cast to your link structure type.
  64. void
  65. link_dispose (struct link_head *token, void *ptr)
  66. pass it the token and a pointer to the structure to be de-allocated.
  67. The memory is returned to the memory manager.