ext_buffer.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>String Buffer Library</title>
  5. <meta charset="utf-8">
  6. <meta name="Copyright" content="Copyright (C) 2005-2021">
  7. <meta name="Language" content="en">
  8. <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
  9. <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
  10. <style type="text/css">
  11. .lib {
  12. vertical-align: middle;
  13. margin-left: 5px;
  14. padding: 0 5px;
  15. font-size: 60%;
  16. border-radius: 5px;
  17. background: #c5d5ff;
  18. color: #000;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div id="site">
  24. <a href="https://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
  25. </div>
  26. <div id="head">
  27. <h1>String Buffer Library</h1>
  28. </div>
  29. <div id="nav">
  30. <ul><li>
  31. <a href="luajit.html">LuaJIT</a>
  32. <ul><li>
  33. <a href="https://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
  34. </li><li>
  35. <a href="install.html">Installation</a>
  36. </li><li>
  37. <a href="running.html">Running</a>
  38. </li></ul>
  39. </li><li>
  40. <a href="extensions.html">Extensions</a>
  41. <ul><li>
  42. <a href="ext_ffi.html">FFI Library</a>
  43. <ul><li>
  44. <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
  45. </li><li>
  46. <a href="ext_ffi_api.html">ffi.* API</a>
  47. </li><li>
  48. <a href="ext_ffi_semantics.html">FFI Semantics</a>
  49. </li></ul>
  50. </li><li>
  51. <a class="current" href="ext_buffer.html">String Buffers</a>
  52. </li><li>
  53. <a href="ext_jit.html">jit.* Library</a>
  54. </li><li>
  55. <a href="ext_c_api.html">Lua/C API</a>
  56. </li><li>
  57. <a href="ext_profiler.html">Profiler</a>
  58. </li></ul>
  59. </li><li>
  60. <a href="status.html">Status</a>
  61. </li><li>
  62. <a href="faq.html">FAQ</a>
  63. </li><li>
  64. <a href="http://wiki.luajit.org/">Wiki <span class="ext">&raquo;</span></a>
  65. </li><li>
  66. <a href="https://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
  67. </li></ul>
  68. </div>
  69. <div id="main">
  70. <p>
  71. The string buffer library allows <b>high-performance manipulation of
  72. string-like data</b>.
  73. </p>
  74. <p>
  75. Unlike Lua strings, which are constants, string buffers are
  76. <b>mutable</b> sequences of 8-bit (binary-transparent) characters. Data
  77. can be stored, formatted and encoded into a string buffer and later
  78. converted, extracted or decoded.
  79. </p>
  80. <p>
  81. The convenient string buffer API simplifies common string manipulation
  82. tasks, that would otherwise require creating many intermediate strings.
  83. String buffers improve performance by eliminating redundant memory
  84. copies, object creation, string interning and garbage collection
  85. overhead. In conjunction with the FFI library, they allow zero-copy
  86. operations.
  87. </p>
  88. <p>
  89. The string buffer libary also includes a high-performance
  90. <a href="serialize">serializer</a> for Lua objects.
  91. </p>
  92. <h2 id="wip" style="color:#ff0000">Work in Progress</h2>
  93. <p>
  94. <b style="color:#ff0000">This library is a work in progress. More
  95. functionality will be added soon.</b>
  96. </p>
  97. <h2 id="use">Using the String Buffer Library</h2>
  98. <p>
  99. The string buffer library is built into LuaJIT by default, but it's not
  100. loaded by default. Add this to the start of every Lua file that needs
  101. one of its functions:
  102. </p>
  103. <pre class="code">
  104. local buffer = require("string.buffer")
  105. </pre>
  106. <p>
  107. The convention for the syntax shown on this page is that <tt>buffer</tt>
  108. refers to the buffer library and <tt>buf</tt> refers to an individual
  109. buffer object.
  110. </p>
  111. <p>
  112. Please note the difference between a Lua function call, e.g.
  113. <tt>buffer.new()</tt> (with a dot) and a Lua method call, e.g.
  114. <tt>buf:reset()</tt> (with a colon).
  115. </p>
  116. <h3 id="buffer_object">Buffer Objects</h3>
  117. <p>
  118. A buffer object is a garbage-collected Lua object. After creation with
  119. <tt>buffer.new()</tt>, it can (and should) be reused for many operations.
  120. When the last reference to a buffer object is gone, it will eventually
  121. be freed by the garbage collector, along with the allocated buffer
  122. space.
  123. </p>
  124. <p>
  125. Buffers operate like a FIFO (first-in first-out) data structure. Data
  126. can be appended (written) to the end of the buffer and consumed (read)
  127. from the front of the buffer. These operations can be freely mixed.
  128. </p>
  129. <p>
  130. The buffer space that holds the characters is managed automatically
  131. &mdash; it grows as needed and already consumed space is recycled. Use
  132. <tt>buffer.new(size)</tt> and <tt>buf:free()</tt>, if you need more
  133. control.
  134. </p>
  135. <p>
  136. The maximum size of a single buffer is the same as the maximum size of a
  137. Lua string, which is slightly below two gigabytes. For huge data sizes,
  138. neither strings nor buffers are the right data structure &mdash; use the
  139. FFI library to directly map memory or files up to the virtual memory
  140. limit of your OS.
  141. </p>
  142. <h3 id="buffer_overview">Buffer Method Overview</h3>
  143. <ul>
  144. <li>
  145. The <tt>buf:put*()</tt>-like methods append (write) characters to the
  146. end of the buffer.
  147. </li>
  148. <li>
  149. The <tt>buf:get*()</tt>-like methods consume (read) characters from the
  150. front of the buffer.
  151. </li>
  152. <li>
  153. Other methods, like <tt>buf:tostring()</tt> only read the buffer
  154. contents, but don't change the buffer.
  155. </li>
  156. <li>
  157. The <tt>buf:set()</tt> method allows zero-copy consumption of a string
  158. or an FFI cdata object as a buffer.
  159. </li>
  160. <li>
  161. The FFI-specific methods allow zero-copy read/write-style operations or
  162. modifying the buffer contents in-place. Please check the
  163. <a href="#ffi_caveats">FFI caveats</a> below, too.
  164. </li>
  165. <li>
  166. Methods that don't need to return anything specific, return the buffer
  167. object itself as a convenience. This allows method chaining, e.g.:
  168. <tt>buf:reset():encode(obj)</tt> or <tt>buf:skip(len):get()</tt>
  169. </li>
  170. </ul>
  171. <h2 id="create">Buffer Creation and Management</h2>
  172. <h3 id="buffer_new"><tt>local buf = buffer.new([size])</tt></h3>
  173. <p>
  174. Creates a new buffer object.
  175. </p>
  176. <p>
  177. The optional <tt>size</tt> argument ensures a minimum initial buffer
  178. size. This is strictly an optimization for cases where the required
  179. buffer size is known beforehand.
  180. </p>
  181. <h3 id="buffer_reset"><tt>buf = buf:reset()</tt></h3>
  182. <p>
  183. Reset (empty) the buffer. The allocated buffer space is not freed and
  184. may be reused.
  185. </p>
  186. <h3 id="buffer_free"><tt>buf = buf:free()</tt></h3>
  187. <p>
  188. The buffer space of the buffer object is freed. The object itself
  189. remains intact, empty and it may be reused.
  190. </p>
  191. <p>
  192. Note: you normally don't need to use this method. The garbage collector
  193. automatically frees the buffer space, when the buffer object is
  194. collected. Use this method, if you need to free the associated memory
  195. immediately.
  196. </p>
  197. <h2 id="write">Buffer Writers</h2>
  198. <h3 id="buffer_put"><tt>buf = buf:put([str|num|obj] [, ...])</tt></h3>
  199. <p>
  200. Appends a string <tt>str</tt>, a number <tt>num</tt> or any object
  201. <tt>obj</tt> with a <tt>__tostring</tt> metamethod to the buffer.
  202. Multiple arguments are appended in the given order.
  203. </p>
  204. <p>
  205. Appending a buffer to a buffer is possible and short-circuited
  206. internally. But it still involves a copy. Better combine the buffer
  207. writes to use a single buffer.
  208. </p>
  209. <h3 id="buffer_putf"><tt>buf = buf:putf(format, ...)</tt></h3>
  210. <p>
  211. Appends the formatted arguments to the buffer. The <tt>format</tt>
  212. string supports the same options as <tt>string.format()</tt>.
  213. </p>
  214. <h3 id="buffer_putcdata"><tt>buf = buf:putcdata(cdata, len)</tt><span class="lib">FFI</span></h3>
  215. <p>
  216. Appends the given <tt>len</tt> number of bytes from the memory pointed
  217. to by the FFI <tt>cdata</tt> object to the buffer. The object needs to
  218. be convertible to a (constant) pointer.
  219. </p>
  220. <h3 id="buffer_set"><tt>buf = buf:set(str)<br>
  221. buf = buf:set(cdata, len)</tt><span class="lib">FFI</span></h3>
  222. <p>
  223. This method allows zero-copy consumption of a string or an FFI cdata
  224. object as a buffer. It stores a reference to the passed string
  225. <tt>str</tt> or the FFI <tt>cdata</tt> object in the buffer. Any buffer
  226. space originally allocated is freed. This is <i>not</i> an append
  227. operation, unlike the <tt>buf:put*()</tt> methods.
  228. </p>
  229. <p>
  230. After calling this method, the buffer behaves as if
  231. <tt>buf:free():put(str)</tt> or <tt>buf:free():put(cdata,&nbsp;len)</tt>
  232. had been called. However, the data is only referenced and not copied, as
  233. long as the buffer is only consumed.
  234. </p>
  235. <p>
  236. In case the buffer is written to later on, the referenced data is copied
  237. and the object reference is removed (copy-on-write semantics).
  238. </p>
  239. <p>
  240. The stored reference is an anchor for the garbage collector and keeps the
  241. originally passed string or FFI cdata object alive.
  242. </p>
  243. <h3 id="buffer_reserve"><tt>ptr, len = buf:reserve(size)</tt><span class="lib">FFI</span><br>
  244. <tt>buf = buf:commit(used)</tt><span class="lib">FFI</span></h3>
  245. <p>
  246. The <tt>reserve</tt> method reserves at least <tt>size</tt> bytes of
  247. write space in the buffer. It returns an <tt>uint8_t&nbsp;*</tt> FFI
  248. cdata pointer <tt>ptr</tt> that points to this space.
  249. </p>
  250. <p>
  251. The available length in bytes is returned in <tt>len</tt>. This is at
  252. least <tt>size</tt> bytes, but may be more to facilitate efficient
  253. buffer growth. You can either make use of the additional space or ignore
  254. <tt>len</tt> and only use <tt>size</tt> bytes.
  255. </p>
  256. <p>
  257. The <tt>commit</tt> method appends the <tt>used</tt> bytes of the
  258. previously returned write space to the buffer data.
  259. </p>
  260. <p>
  261. This pair of methods allows zero-copy use of C read-style APIs:
  262. </p>
  263. <pre class="code">
  264. local MIN_SIZE = 65536
  265. repeat
  266. local ptr, len = buf:reserve(MIN_SIZE)
  267. local n = C.read(fd, ptr, len)
  268. if n == 0 then break end -- EOF.
  269. if n &lt; 0 then error("read error") end
  270. buf:commit(n)
  271. until false
  272. </pre>
  273. <p>
  274. The reserved write space is <i>not</i> initialized. At least the
  275. <tt>used</tt> bytes <b>must</b> be written to before calling the
  276. <tt>commit</tt> method. There's no need to call the <tt>commit</tt>
  277. method, if nothing is added to the buffer (e.g. on error).
  278. </p>
  279. <h2 id="read">Buffer Readers</h2>
  280. <h3 id="buffer_length"><tt>len = #buf</tt></h3>
  281. <p>
  282. Returns the current length of the buffer data in bytes.
  283. </p>
  284. <h3 id="buffer_concat"><tt>res = str|num|buf .. str|num|buf [...]</tt></h3>
  285. <p>
  286. The Lua concatenation operator <tt>..</tt> also accepts buffers, just
  287. like strings or numbers. It always returns a string and not a buffer.
  288. </p>
  289. <p>
  290. Note that although this is supported for convenience, this thwarts one
  291. of the main reasons to use buffers, which is to avoid string
  292. allocations. Rewrite it with <tt>buf:put()</tt> and <tt>buf:get()</tt>.
  293. </p>
  294. <p>
  295. Mixing this with unrelated objects that have a <tt>__concat</tt>
  296. metamethod may not work, since these probably only expect strings.
  297. </p>
  298. <h3 id="buffer_skip"><tt>buf = buf:skip(len)</tt></h3>
  299. <p>
  300. Skips (consumes) <tt>len</tt> bytes from the buffer up to the current
  301. length of the buffer data.
  302. </p>
  303. <h3 id="buffer_get"><tt>str, ... = buf:get([len|nil] [,...])</tt></h3>
  304. <p>
  305. Consumes the buffer data and returns one or more strings. If called
  306. without arguments, the whole buffer data is consumed. If called with a
  307. number, up to <tt>len</tt> bytes are consumed. A <tt>nil</tt> argument
  308. consumes the remaining buffer space (this only makes sense as the last
  309. argument). Multiple arguments consume the buffer data in the given
  310. order.
  311. </p>
  312. <p>
  313. Note: a zero length or no remaining buffer data returns an empty string
  314. and not <tt>nil</tt>.
  315. </p>
  316. <h3 id="buffer_tostring"><tt>str = buf:tostring()<br>
  317. str = tostring(buf)</tt></h3>
  318. <p>
  319. Creates a string from the buffer data, but doesn't consume it. The
  320. buffer remains unchanged.
  321. </p>
  322. <p>
  323. Buffer objects also define a <tt>__tostring</tt> metamethod. This means
  324. buffers can be passed to the global <tt>tostring()</tt> function and
  325. many other functions that accept this in place of strings. The important
  326. internal uses in functions like <tt>io.write()</tt> are short-circuited
  327. to avoid the creation of an intermediate string object.
  328. </p>
  329. <h3 id="buffer_ref"><tt>ptr, len = buf:ref()</tt><span class="lib">FFI</span></h3>
  330. <p>
  331. Returns an <tt>uint8_t&nbsp;*</tt> FFI cdata pointer <tt>ptr</tt> that
  332. points to the buffer data. The length of the buffer data in bytes is
  333. returned in <tt>len</tt>.
  334. </p>
  335. <p>
  336. The returned pointer can be directly passed to C functions that expect a
  337. buffer and a length. You can also do bytewise reads
  338. (<tt>local&nbsp;x&nbsp;=&nbsp;ptr[i]</tt>) or writes
  339. (<tt>ptr[i]&nbsp;=&nbsp;0x40</tt>) of the buffer data.
  340. </p>
  341. <p>
  342. In conjunction with the <tt>skip</tt> method, this allows zero-copy use
  343. of C write-style APIs:
  344. </p>
  345. <pre class="code">
  346. repeat
  347. local ptr, len = buf:ref()
  348. if len == 0 then break end
  349. local n = C.write(fd, ptr, len)
  350. if n &lt; 0 then error("write error") end
  351. buf:skip(n)
  352. until n >= len
  353. </pre>
  354. <p>
  355. Unlike Lua strings, buffer data is <i>not</i> implicitly
  356. zero-terminated. It's not safe to pass <tt>ptr</tt> to C functions that
  357. expect zero-terminated strings. If you're not using <tt>len</tt>, then
  358. you're doing something wrong.
  359. </p>
  360. <h2 id="serialize">Serialization of Lua Objects</h2>
  361. <p>
  362. The following functions and methods allow <b>high-speed serialization</b>
  363. (encoding) of a Lua object into a string and decoding it back to a Lua
  364. object. This allows convenient storage and transport of <b>structured
  365. data</b>.
  366. </p>
  367. <p>
  368. The encoded data is in an <a href="#serialize_format">internal binary
  369. format</a>. The data can be stored in files, binary-transparent
  370. databases or transmitted to other LuaJIT instances across threads,
  371. processes or networks.
  372. </p>
  373. <p>
  374. Encoding speed can reach up to 1 Gigabyte/second on a modern desktop- or
  375. server-class system, even when serializing many small objects. Decoding
  376. speed is mostly constrained by object creation cost.
  377. </p>
  378. <p>
  379. The serializer handles most Lua types, common FFI number types and
  380. nested structures. Functions, thread objects, other FFI cdata, full
  381. userdata and associated metatables cannot be serialized (yet).
  382. </p>
  383. <p>
  384. The encoder serializes nested structures as trees. Multiple references
  385. to a single object will be stored separately and create distinct objects
  386. after decoding. Circular references cause an error.
  387. </p>
  388. <h3 id="serialize_methods">Serialization Functions and Methods</h3>
  389. <h3 id="buffer_encode"><tt>str = buffer.encode(obj)<br>
  390. buf = buf:encode(obj)</tt></h3>
  391. <p>
  392. Serializes (encodes) the Lua object <tt>obj</tt>. The stand-alone
  393. function returns a string <tt>str</tt>. The buffer method appends the
  394. encoding to the buffer.
  395. </p>
  396. <p>
  397. <tt>obj</tt> can be any of the supported Lua types &mdash; it doesn't
  398. need to be a Lua table.
  399. </p>
  400. <p>
  401. This function may throw an error when attempting to serialize
  402. unsupported object types, circular references or deeply nested tables.
  403. </p>
  404. <h3 id="buffer_decode"><tt>obj = buffer.decode(str)<br>
  405. obj = buf:decode()</tt></h3>
  406. <p>
  407. The stand-alone function de-serializes (decodes) the string
  408. <tt>str</tt>, the buffer method de-serializes one object from the
  409. buffer. Both return a Lua object <tt>obj</tt>.
  410. </p>
  411. <p>
  412. The returned object may be any of the supported Lua types &mdash;
  413. even <tt>nil</tt>.
  414. </p>
  415. <p>
  416. This function may throw an error when fed with malformed or incomplete
  417. encoded data. The stand-alone function throws when there's left-over
  418. data after decoding a single top-level object. The buffer method leaves
  419. any left-over data in the buffer.
  420. </p>
  421. <h3 id="serialize_stream">Streaming Serialization</h3>
  422. <p>
  423. In some contexts, it's desirable to do piecewise serialization of large
  424. datasets, also known as <i>streaming</i>.
  425. </p>
  426. <p>
  427. This serialization format can be safely concatenated and supports streaming.
  428. Multiple encodings can simply be appended to a buffer and later decoded
  429. individually:
  430. </p>
  431. <pre class="code">
  432. local buf = buffer.new()
  433. buf:encode(obj1)
  434. buf:encode(obj2)
  435. local copy1 = buf:decode()
  436. local copy2 = buf:decode()
  437. </pre>
  438. <p>
  439. Here's how to iterate over a stream:
  440. </p>
  441. <pre class="code">
  442. while #buf ~= 0 do
  443. local obj = buf:decode()
  444. -- Do something with obj.
  445. end
  446. </pre>
  447. <p>
  448. Since the serialization format doesn't prepend a length to its encoding,
  449. network applications may need to transmit the length, too.
  450. </p>
  451. <h3 id="serialize_format">Serialization Format Specification</h3>
  452. <p>
  453. This serialization format is designed for <b>internal use</b> by LuaJIT
  454. applications. Serialized data is upwards-compatible and portable across
  455. all supported LuaJIT platforms.
  456. </p>
  457. <p>
  458. It's an <b>8-bit binary format</b> and not human-readable. It uses e.g.
  459. embedded zeroes and stores embedded Lua string objects unmodified, which
  460. are 8-bit-clean, too. Encoded data can be safely concatenated for
  461. streaming and later decoded one top-level object at a time.
  462. </p>
  463. <p>
  464. The encoding is reasonably compact, but tuned for maximum performance,
  465. not for minimum space usage. It compresses well with any of the common
  466. byte-oriented data compression algorithms.
  467. </p>
  468. <p>
  469. Although documented here for reference, this format is explicitly
  470. <b>not</b> intended to be a 'public standard' for structured data
  471. interchange across computer languages (like JSON or MessagePack). Please
  472. do not use it as such.
  473. </p>
  474. <p>
  475. The specification is given below as a context-free grammar with a
  476. top-level <tt>object</tt> as the starting point. Alternatives are
  477. separated by the <tt>|</tt> symbol and <tt>*</tt> indicates repeats.
  478. Grouping is implicit or indicated by <tt>{…}</tt>. Terminals are
  479. either plain hex numbers, encoded as bytes, or have a <tt>.format</tt>
  480. suffix.
  481. </p>
  482. <pre>
  483. object → nil | false | true
  484. | null | lightud32 | lightud64
  485. | int | num | tab
  486. | int64 | uint64 | complex
  487. | string
  488. nil → 0x00
  489. false → 0x01
  490. true → 0x02
  491. null → 0x03 // NULL lightuserdata
  492. lightud32 → 0x04 data.I // 32 bit lightuserdata
  493. lightud64 → 0x05 data.L // 64 bit lightuserdata
  494. int → 0x06 int.I // int32_t
  495. num → 0x07 double.L
  496. tab → 0x08 // Empty table
  497. | 0x09 h.U h*{object object} // Key/value hash
  498. | 0x0a a.U a*object // 0-based array
  499. | 0x0b a.U a*object h.U h*{object object} // Mixed
  500. | 0x0c a.U (a-1)*object // 1-based array
  501. | 0x0d a.U (a-1)*object h.U h*{object object} // Mixed
  502. int64 → 0x10 int.L // FFI int64_t
  503. uint64 → 0x11 uint.L // FFI uint64_t
  504. complex → 0x12 re.L im.L // FFI complex
  505. string → (0x20+len).U len*char.B
  506. .B = 8 bit
  507. .I = 32 bit little-endian
  508. .L = 64 bit little-endian
  509. .U = prefix-encoded 32 bit unsigned number n:
  510. 0x00..0xdf → n.B
  511. 0xe0..0x1fdf → (0xe0|(((n-0xe0)>>8)&0x1f)).B ((n-0xe0)&0xff).B
  512. 0x1fe0.. → 0xff n.I
  513. </pre>
  514. <h2 id="error">Error handling</h2>
  515. <p>
  516. Many of the buffer methods can throw an error. Out-of-memory or usage
  517. errors are best caught with an outer wrapper for larger parts of code.
  518. There's not much one can do after that, anyway.
  519. </p>
  520. <p>
  521. OTOH you may want to catch some errors individually. Buffer methods need
  522. to receive the buffer object as the first argument. The Lua colon-syntax
  523. <tt>obj:method()</tt> does that implicitly. But to wrap a method with
  524. <tt>pcall()</tt>, the arguments need to be passed like this:
  525. </p>
  526. <pre class="code">
  527. local ok, err = pcall(buf.encode, buf, obj)
  528. if not ok then
  529. -- Handle error in err.
  530. end
  531. </pre>
  532. <h2 id="ffi_caveats">FFI caveats</h2>
  533. <p>
  534. The string buffer library has been designed to work well together with
  535. the FFI library. But due to the low-level nature of the FFI library,
  536. some care needs to be taken:
  537. </p>
  538. <p>
  539. First, please remember that FFI pointers are zero-indexed. The space
  540. returned by <tt>buf:reserve()</tt> and <tt>buf:ref()</tt> starts at the
  541. returned pointer and ends before <tt>len</tt> bytes after that.
  542. </p>
  543. <p>
  544. I.e. the first valid index is <tt>ptr[0]</tt> and the last valid index
  545. is <tt>ptr[len-1]</tt>. If the returned length is zero, there's no valid
  546. index at all. The returned pointer may even be <tt>NULL</tt>.
  547. </p>
  548. <p>
  549. The space pointed to by the returned pointer is only valid as long as
  550. the buffer is not modified in any way (neither append, nor consume, nor
  551. reset, etc.). The pointer is also not a GC anchor for the buffer object
  552. itself.
  553. </p>
  554. <p>
  555. Buffer data is only guaranteed to be byte-aligned. Casting the returned
  556. pointer to a data type with higher alignment may cause unaligned
  557. accesses. It depends on the CPU architecture whether this is allowed or
  558. not (it's always OK on x86/x64 and mostly OK on other modern
  559. architectures).
  560. </p>
  561. <p>
  562. FFI pointers or references do not count as GC anchors for an underlying
  563. object. E.g. an <tt>array</tt> allocated with <tt>ffi.new()</tt> is
  564. anchored by <tt>buf:set(array,&nbsp;len)</tt>, but not by
  565. <tt>buf:set(array+offset,&nbsp;len)</tt>. The addition of the offset
  566. creates a new pointer, even when the offset is zero. In this case, you
  567. need to make sure there's still a reference to the original array as
  568. long as its contents are in use by the buffer.
  569. </p>
  570. <p>
  571. Even though each LuaJIT VM instance is single-threaded (but you can
  572. create multiple VMs), FFI data structures can be accessed concurrently.
  573. Be careful when reading/writing FFI cdata from/to buffers to avoid
  574. concurrent accesses or modifications. In particular, the memory
  575. referenced by <tt>buf:set(cdata,&nbsp;len)</tt> must not be modified
  576. while buffer readers are working on it. Shared, but read-only memory
  577. mappings of files are OK, but only if the file does not change.
  578. </p>
  579. <br class="flush">
  580. </div>
  581. <div id="foot">
  582. <hr class="hide">
  583. Copyright &copy; 2005-2021
  584. <span class="noprint">
  585. &middot;
  586. <a href="contact.html">Contact</a>
  587. </span>
  588. </div>
  589. </body>
  590. </html>