mem_util.lyx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #LyX 1.3 created this file. For more info see http://www.lyx.org/
  2. \lyxformat 221
  3. \textclass article
  4. \language english
  5. \inputencoding auto
  6. \fontscheme default
  7. \graphics default
  8. \paperfontsize default
  9. \spacing single
  10. \papersize letterpaper
  11. \paperpackage a4
  12. \use_geometry 1
  13. \use_amsmath 0
  14. \use_natbib 0
  15. \use_numerical_citations 0
  16. \paperorientation portrait
  17. \leftmargin 1in
  18. \topmargin 1in
  19. \rightmargin 1in
  20. \bottommargin 0.75in
  21. \secnumdepth 3
  22. \tocdepth 3
  23. \paragraph_separation indent
  24. \defskip medskip
  25. \quotes_language english
  26. \quotes_times 2
  27. \papercolumns 1
  28. \papersides 1
  29. \paperpagestyle default
  30. \layout Title
  31. Documentation for MCell Memory Utilities
  32. \layout Author
  33. Rex Kerr
  34. \layout Standard
  35. The memory utilities can be found in the files
  36. \family typewriter
  37. mem_util.h
  38. \family default
  39. and
  40. \family typewriter
  41. mem_util.c
  42. \layout Section
  43. stack_helper
  44. \layout Standard
  45. The
  46. \family typewriter
  47. stack_helper
  48. \family default
  49. struct and functions implement a hybrid array/linked-list stack of a set
  50. of items of the same size.
  51. The main struct has an array of data of a specified size, plus a pointer
  52. to the next part of the stack should the first part overflow.
  53. There are seven functions for dealing with stacks:
  54. \layout LyX-Code
  55. struct stack_helper* create_stack(int size,int length);
  56. \layout LyX-Code
  57. void stack_push(struct stack_helper *sh,void *d);
  58. \layout LyX-Code
  59. void stack_pop(struct stack_helper *sh, void *d);
  60. \layout LyX-Code
  61. void stack_dump(struct stack_helper *sh);
  62. \layout LyX-Code
  63. inline int stack_size(struct stack_helper *sh);
  64. \layout LyX-Code
  65. void* stack_access(struct stack_helper *sh,int n);
  66. \layout LyX-Code
  67. void delete_stack(struct stack_helper *sh);
  68. \layout Standard
  69. To start, one creates a new stack using the
  70. \family typewriter
  71. create_stack
  72. \family default
  73. function.
  74. The first argument is the size of your data structure (e.g.
  75. \family typewriter
  76. sizeof(struct my_struct)
  77. \family default
  78. ).
  79. The second argument is the number of elements in an array of that data
  80. structure.
  81. This number should be chosen small enough to not overburden memory, but
  82. large enough so the stack can function primarily with array access rather
  83. than with list-traversal.
  84. \layout Standard
  85. You can then push and pop items of that data type onto and off of the stack
  86. using the
  87. \family typewriter
  88. stack_push
  89. \family default
  90. and
  91. \family typewriter
  92. stack_pop
  93. \family default
  94. functions.
  95. Note that these
  96. \emph on
  97. copy
  98. \emph default
  99. the data, so stacks are best used for small structs or other types.
  100. \layout Standard
  101. If you wish to clear out the stack, use
  102. \family typewriter
  103. stack_dump
  104. \family default
  105. .
  106. To see the current size of the stack, use
  107. \family typewriter
  108. stack_size
  109. \family default
  110. .
  111. (Zero means the stack is empty.) To get a pointer to one element of the
  112. stack, use
  113. \family typewriter
  114. stack_access
  115. \family default
  116. .
  117. The oldest item on the stack has an index of
  118. \family typewriter
  119. 0
  120. \family default
  121. ; the most recent has an index of
  122. \family typewriter
  123. stack_size(...)-1
  124. \family default
  125. .
  126. \layout Standard
  127. Calling
  128. \family typewriter
  129. delete_stack
  130. \family default
  131. will delete everything you've pushed onto the stack, and will free the
  132. stack_helper itself.
  133. If you wish to only empty the stack but keep using it, use
  134. \family typewriter
  135. stack_dump
  136. \family default
  137. instead.
  138. \layout Standard
  139. Note: stacks are slow in the current implementation if the stack is many
  140. times longer than the length of the array, as it has to wade down a long
  141. linked list.
  142. Need to fix this.
  143. (Easy enough, just swap so the first thing always is the one with space!)
  144. Index-based access will always be slow, though (can't avoid traversing
  145. the list).
  146. \layout Section
  147. mem_helper
  148. \layout Standard
  149. The
  150. \family typewriter
  151. mem_helper
  152. \family default
  153. struct and functions implement a hybrid array/linked-list block-memory
  154. allocation specifically for linked lists.
  155. The main struct has an array of data of a specified size, plus a pointer
  156. to the next allocation block should the first part run out of space.
  157. It also maintains a linked list of list elements that have been deallocated
  158. so that they can be reused.
  159. There are five functions for this utility:
  160. \layout LyX-Code
  161. struct mem_helper* create_mem(int size,int length);
  162. \layout LyX-Code
  163. void* mem_get(struct mem_helper *mh);
  164. \layout LyX-Code
  165. void mem_put(struct mem_helper *mh,void *defunct);
  166. \layout LyX-Code
  167. void mem_put_list(struct mem_helper *mh,void *defunct);
  168. \layout LyX-Code
  169. void delete_mem(struct mem_helper *mh);
  170. \layout Standard
  171. To start, one creates a new helper with the create_mem function.
  172. The first argument is the size of your data structure (e.g.
  173. \family typewriter
  174. sizeof(struct my_struct)
  175. \family default
  176. ) and the second is the number of those structures to allocate in each chunk.
  177. \layout Standard
  178. You then can use
  179. \family typewriter
  180. mem_get
  181. \family default
  182. in place of
  183. \family typewriter
  184. malloc
  185. \family default
  186. to get a pointer to the start of a data structure, and
  187. \family typewriter
  188. mem_put
  189. \family default
  190. instead of
  191. \family typewriter
  192. free
  193. \family default
  194. when you are done with one of your list elements.
  195. If you have a linked list and you wish to free all of them, use
  196. \family typewriter
  197. mem_put_list
  198. \family default
  199. on the head of the linked list.
  200. When you're done with everything you've created with that helper, call
  201. \family typewriter
  202. delete_mem
  203. \family default
  204. and all memory you have allocated, plus the
  205. \family typewriter
  206. mem_helper
  207. \family default
  208. struct itself, will be freed.
  209. \layout Section
  210. temp_mem
  211. \layout Standard
  212. If you want to create a bunch of objects using
  213. \family typewriter
  214. malloc
  215. \family default
  216. and don't want to worry about freeing them all individually, use the
  217. \family typewriter
  218. temp_mem
  219. \family default
  220. struct and functions.
  221. There are only three functions:
  222. \layout LyX-Code
  223. struct temp_mem* setup_temp_mem(int length);
  224. \layout LyX-Code
  225. void* temp_malloc(size_t size,struct temp_mem *list);
  226. \layout LyX-Code
  227. void free_temp(struct temp_mem *list);
  228. \layout Standard
  229. Start off by calling
  230. \family typewriter
  231. setup_temp_mem
  232. \family default
  233. with an argument that estimates the number of separate items you'll be
  234. mallocing (the pointers will be stored on a
  235. \family typewriter
  236. stack_helper
  237. \family default
  238. stack).
  239. Then, just use
  240. \family typewriter
  241. temp_malloc
  242. \family default
  243. instead of
  244. \family typewriter
  245. malloc
  246. \family default
  247. , and when you're done with everything you've
  248. \family typewriter
  249. temp_malloc
  250. \family default
  251. 'ed, call
  252. \family typewriter
  253. free_temp
  254. \family default
  255. .
  256. Simple!
  257. \layout Section
  258. counter_helper
  259. \layout Standard
  260. The
  261. \family typewriter
  262. counter_helper
  263. \family default
  264. struct and functions are a way to make a set (in the mathematical sense)
  265. out of a list of items.
  266. In particular,
  267. \family typewriter
  268. counter_helper
  269. \family default
  270. will find identical items and keep track of the number of that type of
  271. item rather than storing each one individually.
  272. This numbering is kept track of in the
  273. \family typewriter
  274. counter_header
  275. \family default
  276. struct.
  277. The following functions are for use with counter_helper:
  278. \layout LyX-Code
  279. struct counter_helper* create_counter(int size,int length);
  280. \layout LyX-Code
  281. void counter_add(struct counter_helper *ch,void *data);
  282. \layout LyX-Code
  283. void counter_reset(struct counter_helper *ch);
  284. \layout LyX-Code
  285. struct counter_header* counter_iterator(struct counter_helper *ch);
  286. \layout LyX-Code
  287. struct counter_header* counter_next_entry(struct counter_header *c);
  288. \layout LyX-Code
  289. void counter_read(struct counter_helper *ch,struct counter_header *c,void
  290. *data);
  291. \layout LyX-Code
  292. void delete_counter(struct counter_helper *ch);
  293. \layout Standard
  294. As usual, you start with
  295. \family typewriter
  296. create_counter
  297. \family default
  298. and specify the size of your struct and the number of items to allocate
  299. at once.
  300. (
  301. \family typewriter
  302. counter_helper
  303. \family default
  304. uses
  305. \family typewriter
  306. mem_helper
  307. \family default
  308. .) You can then add items using counter_add, where the items will be binned
  309. into groups and counted as you go.
  310. This method
  311. \emph on
  312. copies
  313. \emph default
  314. the data from the individual items.
  315. (This is implemented using linked lists and therefore is slow for large
  316. numbers of items! If you want to throw away the items you've collected
  317. so far, use
  318. \family typewriter
  319. counter_reset
  320. \family default
  321. .
  322. \layout Standard
  323. Once you've added all the items you wish to (or before, if you please),
  324. you can traverse the counted set of items by calling
  325. \family typewriter
  326. counter_iterator
  327. \family default
  328. to point to the first item in the set (returns a
  329. \family typewriter
  330. counter_header
  331. \family default
  332. as an iterator), and then
  333. \family typewriter
  334. counter_next_entry
  335. \family default
  336. on that iterator to get the next one.
  337. If you want to read out the data stored at a particular location, use
  338. \family typewriter
  339. counter_read
  340. \family default
  341. to copy the data in the counter into the pointer you provide.
  342. \layout Standard
  343. Finally, when you're done,
  344. \family typewriter
  345. delete_counter
  346. \family default
  347. will delete the
  348. \family typewriter
  349. counter_helper
  350. \family default
  351. and everything contained within.
  352. None of the items you added will be deleted, since
  353. \family typewriter
  354. counter_helper
  355. \family default
  356. creates copies of the data rather than using the originals.
  357. \layout LyX-Code
  358. \the_end