Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.60 KB | None | 0 0
  1. (mercurial wasn't giving me a parsimonious diff.) Summary: move manual.html's
  2. description of the custom allocator from the lua_Alloc section to lua_newstate,
  3. and briefly outline the risks associated with using lua_setallocf.
  4.  
  5. % diff -c manual.html manual.new.html
  6. *** manual.html 2012-01-02 12:06:35.318316134 -0500
  7. --- manual.new.html 2012-01-02 12:03:49.337493267 -0500
  8. ***************
  9. *** 2848,2854 ****
  10. The type of the memory-allocation function used by Lua states.
  11. The allocator function must provide a
  12. functionality similar to <code>realloc</code>,
  13. ! but not exactly the same.
  14. Its arguments are
  15. <code>ud</code>, an opaque pointer passed to <a
  16. href="#lua_newstate"><code>lua_newstate</code></a>;
  17. <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
  18. --- 2848,2857 ----
  19. The type of the memory-allocation function used by Lua states.
  20. The allocator function must provide a
  21. functionality similar to <code>realloc</code>,
  22. ! but not exactly the same. <b>Its semantics differ from those of the
  23. ! custom allocator described
  24. ! in <a href="#lua_newstate"><code>lua_newstate</code></a></b>, even
  25. ! though they have the same signature.
  26. Its arguments are
  27. <code>ud</code>, an opaque pointer passed to <a
  28. href="#lua_newstate"><code>lua_newstate</code></a>;
  29. <code>ptr</code>, a pointer to the block being allocated/reallocated/freed;
  30. ***************
  31. *** 2874,2923 ****
  32. Lua is allocating memory for something else.
  33.  
  34.  
  35. - <p>
  36. - Lua assumes the following behavior from the allocator function:
  37. -
  38. -
  39. - <p>
  40. - When <code>nsize</code> is zero,
  41. - the allocator should behave like <code>free</code>
  42. - and return <code>NULL</code>.
  43. -
  44. -
  45. - <p>
  46. - When <code>nsize</code> is not zero,
  47. - the allocator should behave like <code>realloc</code>.
  48. - The allocator returns <code>NULL</code>
  49. - if and only if it cannot fulfill the request.
  50. - Lua assumes that the allocator never fails when
  51. - <code>osize >= nsize</code>.
  52. -
  53. -
  54. - <p>
  55. - Here is a simple implementation for the allocator function.
  56. - It is used in the auxiliary library by <a
  57. href="#luaL_newstate"><code>luaL_newstate</code></a>.
  58. -
  59. - <pre>
  60. - static void *l_alloc (void *ud, void *ptr, size_t osize,
  61. - size_t nsize) {
  62. - (void)ud; (void)osize; /* not used */
  63. - if (nsize == 0) {
  64. - free(ptr);
  65. - return NULL;
  66. - }
  67. - else
  68. - return realloc(ptr, nsize);
  69. - }
  70. - </pre><p>
  71. - Note that Standard C ensures
  72. - that <code>free(NULL)</code> has no effect and that
  73. - <code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.
  74. - This code assumes that <code>realloc</code> does not fail when shrinking a block.
  75. - (Although Standard C does not ensure this behavior,
  76. - it seems to be a safe assumption.)
  77. -
  78. -
  79. -
  80.  
  81.  
  82. <hr><h3><a name="lua_arith"><code>lua_arith</code></a></h3><p>
  83. --- 2877,2882 ----
  84. ***************
  85. *** 3735,3743 ****
  86. --- 3694,3740 ----
  87. The second argument, <code>ud</code>, is an opaque pointer that Lua
  88. passes to the allocator in every call.
  89.  
  90. + <p>
  91. + Lua assumes the following behavior from the allocator function:
  92. +
  93.  
  94. + <p>
  95. + When <code>nsize</code> is zero,
  96. + the allocator should behave like <code>free</code>
  97. + and return <code>NULL</code>.
  98. +
  99. +
  100. + <p>
  101. + When <code>nsize</code> is not zero,
  102. + the allocator should behave like <code>realloc</code>.
  103. + The allocator returns <code>NULL</code>
  104. + if and only if it cannot fulfill the request.
  105. + Lua assumes that the allocator never fails when
  106. + <code>osize >= nsize</code>.
  107.  
  108.  
  109. + <p>
  110. + Here is a simple implementation for the allocator function.
  111. + It is used in the auxiliary library by <a
  112. href="#luaL_newstate"><code>luaL_newstate</code></a>.
  113. +
  114. + <pre>
  115. + static void *l_alloc (void *ud, void *ptr, size_t osize,
  116. + size_t nsize) {
  117. + (void)ud; (void)osize; /* not used */
  118. + if (nsize == 0) {
  119. + free(ptr);
  120. + return NULL;
  121. + }
  122. + else
  123. + return realloc(ptr, nsize);
  124. + }
  125. + </pre><p>
  126. + Note that Standard C ensures
  127. + that <code>free(NULL)</code> has no effect and that
  128. + <code>realloc(NULL, size)</code> is equivalent to <code>malloc(size)</code>.
  129. + This code assumes that <code>realloc</code> does not fail when shrinking a block.
  130. + (Although Standard C does not ensure this behavior,
  131. + it seems to be a safe assumption.)
  132.  
  133. <hr><h3><a name="lua_newtable"><code>lua_newtable</code></a></h3><p>
  134. <span class="apii">[-0, +1, <em>m</em>]</span>
  135. ***************
  136. *** 4448,4459 ****
  137. <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
  138.  
  139. <p>
  140. ! Changes the allocator function of a given state to <code>f</code>
  141. ! with user data <code>ud</code>.
  142. !
  143. !
  144. !
  145. !
  146.  
  147. <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
  148. <span class="apii">[-1, +0, <em>e</em>]</span>
  149. --- 4445,4457 ----
  150. <pre>void lua_setallocf (lua_State *L, lua_Alloc f, void *ud);</pre>
  151.  
  152. <p>
  153. ! Changes the allocator function of a given state to <code>f</code> with
  154. ! user data <code>ud</code>.
  155. ! See <a href="#lua_newstate"><code>lua_newstate</code></a> for the
  156. ! semantics of this allocator. Take care when assigning a new allocator
  157. ! with <code>lua_setallocf</code>, as by then the allocator passed
  158. ! to <code>lua_newstate</code> has been already used. Any difference in
  159. ! the allocators' memory layouts is likely cause bugs in obscure ways.
  160.  
  161. <hr><h3><a name="lua_setfield"><code>lua_setfield</code></a></h3><p>
  162. <span class="apii">[-1, +0, <em>e</em>]</span>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement