re05.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>&lt;GBytes&gt;: </title>
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.2">
  7. <link rel="home" href="index.html" title="">
  8. <link rel="up" href="ch01.html" title="GLib">
  9. <link rel="prev" href="re04.html" title="&lt;GByteArray&gt;">
  10. <link rel="next" href="re06.html" title="&lt;GChecksum&gt;">
  11. <meta name="generator" content="GTK-Doc V1.33.1 (XML mode)">
  12. <link rel="stylesheet" href="style.css" type="text/css">
  13. </head>
  14. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  15. <table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
  16. <td width="100%" align="left" class="shortcuts"></td>
  17. <td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
  18. <td><img src="up-insensitive.png" width="16" height="16" border="0"></td>
  19. <td><a accesskey="p" href="re04.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
  20. <td><a accesskey="n" href="re06.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
  21. </tr></table>
  22. <div class="refentry">
  23. <a name="id-1.1.6"></a><div class="titlepage"></div>
  24. <div class="refnamediv">
  25. <h2>&lt;GBytes&gt;</h2>
  26. <p>&lt;GBytes&gt;</p>
  27. </div>
  28. <div class="refsect1">
  29. <a name="id-1.1.6.2"></a><h2>Description</h2>
  30. <p>A simple refcounted data type representing an immutable sequence of zero or
  31. more bytes from an unspecified origin.
  32. </p>
  33. <p>The purpose of a <span class="type">GBytes</span> is to keep the memory region that it holds
  34. alive for as long as anyone holds a reference to the bytes. When
  35. the last reference count is dropped, the memory is released. Multiple
  36. unrelated callers can use byte data in the <span class="type">GBytes</span> without coordinating
  37. their activities, resting assured that the byte data will not change or
  38. move while they hold a reference.
  39. </p>
  40. <p>A <span class="type">GBytes</span> can come from many different origins that may have
  41. different procedures for freeing the memory region. Examples are
  42. memory from <code class="function">g_malloc()</code>, from memory slices, from a <span class="type">GMappedFile</span> or
  43. memory from other allocators.
  44. </p>
  45. <p><span class="type">GBytes</span> work well as keys in <span class="type">GHashTable</span>. Use <code class="function">g_bytes_equal()</code> and
  46. <code class="function">g_bytes_hash()</code> as parameters to <code class="function">g_hash_table_new()</code> or <code class="function">g_hash_table_new_full()</code>.
  47. <span class="type">GBytes</span> can also be used as keys in a <span class="type">GTree</span> by passing the <code class="function">g_bytes_compare()</code>
  48. function to <code class="function">g_tree_new()</code>.
  49. </p>
  50. <p>The data pointed to by this bytes must not be modified. For a mutable
  51. array of bytes see <span class="type">GByteArray</span>. Use <code class="function">g_bytes_unref_to_array()</code> to create a
  52. mutable array for a <span class="type">GBytes</span> sequence. To create an immutable <span class="type">GBytes</span> from
  53. a mutable <span class="type">GByteArray</span>, use the <code class="function">g_byte_array_free_to_bytes()</code> function.</p>
  54. </div>
  55. <div class="refsect1">
  56. <a name="id-1.1.6.3"></a><h2>Functions</h2>
  57. <div class="refsect2">
  58. <a name="id-1.1.6.3.2"></a><h3>compare</h3>
  59. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:compare self bytes2))
  60. </pre></div>
  61. <p>Compares the two <span class="type">GBytes</span> values.
  62. </p>
  63. <p>This function can be used to sort GBytes instances in lexicographical order.
  64. </p>
  65. <p>If <em class="parameter"><code>bytes1</code></em> and <em class="parameter"><code>bytes2</code></em> have different length but the shorter one is a
  66. prefix of the longer one then the shorter one is considered to be less than
  67. the longer one. Otherwise the first byte where both differ is used for
  68. comparison. If <em class="parameter"><code>bytes1</code></em> has a smaller value at that position it is
  69. considered less, otherwise greater than <em class="parameter"><code>bytes2</code></em>.</p>
  70. <div class="refsect3">
  71. <a name="id-1.1.6.3.2.6"></a><h4>Parameters</h4>
  72. <div class="informaltable"><table>
  73. <tr>
  74. <td class="parameter_name"><p>bytes1</p></td>
  75. <td class="parameter_description">
  76. <p>a pointer to a <span class="type">GBytes</span></p>
  77. <p>Passed as <code class="code">self</code></p>
  78. </td>
  79. </tr>
  80. <tr>
  81. <td class="parameter_name"><p>bytes2</p></td>
  82. <td class="parameter_description">
  83. <p>a pointer to a <span class="type">GBytes</span> to compare with <em class="parameter"><code>bytes1</code></em></p>
  84. <p>Passed as <code class="code">bytes2</code></p>
  85. </td>
  86. </tr>
  87. </table></div>
  88. </div>
  89. </div>
  90. <div class="refsect2">
  91. <a name="id-1.1.6.3.3"></a><h3>equal?</h3>
  92. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:equal? self bytes2))
  93. </pre></div>
  94. <p>Compares the two <span class="type">GBytes</span> values being pointed to and returns
  95. <code class="constant">TRUE</code> if they are equal.
  96. </p>
  97. <p>This function can be passed to <code class="function">g_hash_table_new()</code> as the <em class="parameter"><code>key_equal_func</code></em>
  98. parameter, when using non-<code class="constant">NULL</code> <span class="type">GBytes</span> pointers as keys in a <span class="type">GHashTable</span>.</p>
  99. <div class="refsect3">
  100. <a name="id-1.1.6.3.3.5"></a><h4>Parameters</h4>
  101. <div class="informaltable"><table>
  102. <tr>
  103. <td class="parameter_name"><p>bytes1</p></td>
  104. <td class="parameter_description">
  105. <p>a pointer to a <span class="type">GBytes</span></p>
  106. <p>Passed as <code class="code">self</code></p>
  107. </td>
  108. </tr>
  109. <tr>
  110. <td class="parameter_name"><p>bytes2</p></td>
  111. <td class="parameter_description">
  112. <p>a pointer to a <span class="type">GBytes</span> to compare with <em class="parameter"><code>bytes1</code></em></p>
  113. <p>Passed as <code class="code">bytes2</code></p>
  114. </td>
  115. </tr>
  116. </table></div>
  117. </div>
  118. </div>
  119. <div class="refsect2">
  120. <a name="id-1.1.6.3.4"></a><h3>get-data</h3>
  121. <div class="informalexample"><pre class="programlisting">(define-values (%return size) (bytes:get-data self))
  122. </pre></div>
  123. <p>Get the byte data in the <span class="type">GBytes</span>. This data should not be modified.
  124. </p>
  125. <p>This function will always return the same pointer for a given <span class="type">GBytes</span>.
  126. </p>
  127. <p><code class="constant">NULL</code> may be returned if <em class="parameter"><code>size</code></em> is 0. This is not guaranteed, as the <span class="type">GBytes</span>
  128. may represent an empty string with <em class="parameter"><code>data</code></em> non-<code class="constant">NULL</code> and <em class="parameter"><code>size</code></em> as 0. <code class="constant">NULL</code> will
  129. not be returned if <em class="parameter"><code>size</code></em> is non-zero.</p>
  130. <div class="refsect3">
  131. <a name="id-1.1.6.3.4.6"></a><h4>Parameters</h4>
  132. <div class="informaltable"><table>
  133. <tr>
  134. <td class="parameter_name"><p>bytes</p></td>
  135. <td class="parameter_description">
  136. <p>a <span class="type">GBytes</span></p>
  137. <p>Passed as <code class="code">self</code></p>
  138. </td>
  139. </tr>
  140. <tr>
  141. <td class="parameter_name"><p>size</p></td>
  142. <td class="parameter_description">
  143. <p>location to return size of byte data</p>
  144. <p>Inferred from <code class="code">%return</code></p>
  145. </td>
  146. </tr>
  147. </table></div>
  148. </div>
  149. </div>
  150. <div class="refsect2">
  151. <a name="id-1.1.6.3.5"></a><h3>get-region</h3>
  152. <div class="informalexample"><pre class="programlisting">(define-values
  153. (%return)
  154. (bytes:get-region self element-size offset n-elements))
  155. </pre></div>
  156. <p>Undocumented</p>
  157. <div class="refsect3">
  158. <a name="id-1.1.6.3.5.4"></a><h4>Parameters</h4>
  159. <div class="informaltable"><table>
  160. <tr>
  161. <td class="parameter_name"><p>bytes</p></td>
  162. <td class="parameter_description">
  163. <p></p>
  164. <p>Passed as <code class="code">self</code></p>
  165. </td>
  166. </tr>
  167. <tr>
  168. <td class="parameter_name"><p>element_size</p></td>
  169. <td class="parameter_description">
  170. <p></p>
  171. <p>Passed as <code class="code">element-size</code></p>
  172. </td>
  173. </tr>
  174. <tr>
  175. <td class="parameter_name"><p>offset</p></td>
  176. <td class="parameter_description">
  177. <p></p>
  178. <p>Passed as <code class="code">offset</code></p>
  179. </td>
  180. </tr>
  181. <tr>
  182. <td class="parameter_name"><p>n_elements</p></td>
  183. <td class="parameter_description">
  184. <p></p>
  185. <p>Passed as <code class="code">n-elements</code></p>
  186. </td>
  187. </tr>
  188. </table></div>
  189. </div>
  190. </div>
  191. <div class="refsect2">
  192. <a name="id-1.1.6.3.6"></a><h3>get-size</h3>
  193. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:get-size self))
  194. </pre></div>
  195. <p>Get the size of the byte data in the <span class="type">GBytes</span>.
  196. </p>
  197. <p>This function will always return the same value for a given <span class="type">GBytes</span>.</p>
  198. <div class="refsect3">
  199. <a name="id-1.1.6.3.6.5"></a><h4>Parameters</h4>
  200. <div class="informaltable"><table><tr>
  201. <td class="parameter_name"><p>bytes</p></td>
  202. <td class="parameter_description">
  203. <p>a <span class="type">GBytes</span></p>
  204. <p>Passed as <code class="code">self</code></p>
  205. </td>
  206. </tr></table></div>
  207. </div>
  208. </div>
  209. <div class="refsect2">
  210. <a name="id-1.1.6.3.7"></a><h3>hash</h3>
  211. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:hash self))
  212. </pre></div>
  213. <p>Creates an integer hash code for the byte data in the <span class="type">GBytes</span>.
  214. </p>
  215. <p>This function can be passed to <code class="function">g_hash_table_new()</code> as the <em class="parameter"><code>key_hash_func</code></em>
  216. parameter, when using non-<code class="constant">NULL</code> <span class="type">GBytes</span> pointers as keys in a <span class="type">GHashTable</span>.</p>
  217. <div class="refsect3">
  218. <a name="id-1.1.6.3.7.5"></a><h4>Parameters</h4>
  219. <div class="informaltable"><table><tr>
  220. <td class="parameter_name"><p>bytes</p></td>
  221. <td class="parameter_description">
  222. <p>a pointer to a <span class="type">GBytes</span> key</p>
  223. <p>Passed as <code class="code">self</code></p>
  224. </td>
  225. </tr></table></div>
  226. </div>
  227. </div>
  228. <div class="refsect2">
  229. <a name="id-1.1.6.3.8"></a><h3>new-from-bytes</h3>
  230. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:new-from-bytes self offset length))
  231. </pre></div>
  232. <p>Creates a <span class="type">GBytes</span> which is a subsection of another <span class="type">GBytes</span>. The <em class="parameter"><code>offset</code></em> +
  233. <em class="parameter"><code>length</code></em> may not be longer than the size of <em class="parameter"><code>bytes</code></em>.
  234. </p>
  235. <p>A reference to <em class="parameter"><code>bytes</code></em> will be held by the newly created <span class="type">GBytes</span> until
  236. the byte data is no longer needed.
  237. </p>
  238. <p>Since 2.56, if <em class="parameter"><code>offset</code></em> is 0 and <em class="parameter"><code>length</code></em> matches the size of <em class="parameter"><code>bytes</code></em>, then
  239. <em class="parameter"><code>bytes</code></em> will be returned with the reference count incremented by 1. If <em class="parameter"><code>bytes</code></em>
  240. is a slice of another <span class="type">GBytes</span>, then the resulting <span class="type">GBytes</span> will reference
  241. the same <span class="type">GBytes</span> instead of <em class="parameter"><code>bytes</code></em>. This allows consumers to simplify the
  242. usage of <span class="type">GBytes</span> when asynchronously writing to streams.</p>
  243. <div class="refsect3">
  244. <a name="id-1.1.6.3.8.6"></a><h4>Parameters</h4>
  245. <div class="informaltable"><table>
  246. <tr>
  247. <td class="parameter_name"><p>bytes</p></td>
  248. <td class="parameter_description">
  249. <p>a <span class="type">GBytes</span></p>
  250. <p>Passed as <code class="code">self</code></p>
  251. </td>
  252. </tr>
  253. <tr>
  254. <td class="parameter_name"><p>offset</p></td>
  255. <td class="parameter_description">
  256. <p>offset which subsection starts at</p>
  257. <p>Passed as <code class="code">offset</code></p>
  258. </td>
  259. </tr>
  260. <tr>
  261. <td class="parameter_name"><p>length</p></td>
  262. <td class="parameter_description">
  263. <p>length of subsection</p>
  264. <p>Passed as <code class="code">length</code></p>
  265. </td>
  266. </tr>
  267. </table></div>
  268. </div>
  269. </div>
  270. <div class="refsect2">
  271. <a name="id-1.1.6.3.9"></a><h3>ref</h3>
  272. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:ref self))
  273. </pre></div>
  274. <p>Increase the reference count on <em class="parameter"><code>bytes</code></em>.</p>
  275. <div class="refsect3">
  276. <a name="id-1.1.6.3.9.4"></a><h4>Parameters</h4>
  277. <div class="informaltable"><table><tr>
  278. <td class="parameter_name"><p>bytes</p></td>
  279. <td class="parameter_description">
  280. <p>a <span class="type">GBytes</span></p>
  281. <p>Passed as <code class="code">self</code></p>
  282. </td>
  283. </tr></table></div>
  284. </div>
  285. </div>
  286. <div class="refsect2">
  287. <a name="id-1.1.6.3.10"></a><h3>unref</h3>
  288. <div class="informalexample"><pre class="programlisting">(define-values () (bytes:unref self))
  289. </pre></div>
  290. <p>Releases a reference on <em class="parameter"><code>bytes</code></em>. This may result in the bytes being
  291. freed. If <em class="parameter"><code>bytes</code></em> is <code class="constant">NULL</code>, it will return immediately.</p>
  292. <div class="refsect3">
  293. <a name="id-1.1.6.3.10.4"></a><h4>Parameters</h4>
  294. <div class="informaltable"><table><tr>
  295. <td class="parameter_name"><p>bytes</p></td>
  296. <td class="parameter_description">
  297. <p>a <span class="type">GBytes</span></p>
  298. <p>Passed as <code class="code">self</code></p>
  299. </td>
  300. </tr></table></div>
  301. </div>
  302. </div>
  303. <div class="refsect2">
  304. <a name="id-1.1.6.3.11"></a><h3>unref-to-array</h3>
  305. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:unref-to-array self))
  306. </pre></div>
  307. <p>Unreferences the bytes, and returns a new mutable <span class="type">GByteArray</span> containing
  308. the same byte data.
  309. </p>
  310. <p>As an optimization, the byte data is transferred to the array without copying
  311. if this was the last reference to bytes and bytes was created with
  312. <code class="function">g_bytes_new()</code>, <code class="function">g_bytes_new_take()</code> or <code class="function">g_byte_array_free_to_bytes()</code>. In all
  313. other cases the data is copied.</p>
  314. <div class="refsect3">
  315. <a name="id-1.1.6.3.11.5"></a><h4>Parameters</h4>
  316. <div class="informaltable"><table><tr>
  317. <td class="parameter_name"><p>bytes</p></td>
  318. <td class="parameter_description">
  319. <p>a <span class="type">GBytes</span></p>
  320. <p>Passed as <code class="code">self</code></p>
  321. </td>
  322. </tr></table></div>
  323. </div>
  324. </div>
  325. <div class="refsect2">
  326. <a name="id-1.1.6.3.12"></a><h3>unref-to-data</h3>
  327. <div class="informalexample"><pre class="programlisting">(define-values (%return size) (bytes:unref-to-data self))
  328. </pre></div>
  329. <p>Unreferences the bytes, and returns a pointer the same byte data
  330. contents.
  331. </p>
  332. <p>As an optimization, the byte data is returned without copying if this was
  333. the last reference to bytes and bytes was created with <code class="function">g_bytes_new()</code>,
  334. <code class="function">g_bytes_new_take()</code> or <code class="function">g_byte_array_free_to_bytes()</code>. In all other cases the
  335. data is copied.</p>
  336. <div class="refsect3">
  337. <a name="id-1.1.6.3.12.5"></a><h4>Parameters</h4>
  338. <div class="informaltable"><table>
  339. <tr>
  340. <td class="parameter_name"><p>bytes</p></td>
  341. <td class="parameter_description">
  342. <p>a <span class="type">GBytes</span></p>
  343. <p>Passed as <code class="code">self</code></p>
  344. </td>
  345. </tr>
  346. <tr>
  347. <td class="parameter_name"><p>size</p></td>
  348. <td class="parameter_description">
  349. <p>location to place the length of the returned data</p>
  350. <p>Inferred from <code class="code">%return</code></p>
  351. </td>
  352. </tr>
  353. </table></div>
  354. </div>
  355. </div>
  356. <div class="refsect2">
  357. <a name="id-1.1.6.3.13"></a><h3>bytes:new-take</h3>
  358. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:new-take data))
  359. </pre></div>
  360. <p>Undocumented</p>
  361. <div class="refsect3">
  362. <a name="id-1.1.6.3.13.4"></a><h4>Parameters</h4>
  363. <div class="informaltable"><table>
  364. <tr>
  365. <td class="parameter_name"><p>data</p></td>
  366. <td class="parameter_description">
  367. <p></p>
  368. <p>Passed as <code class="code">data</code></p>
  369. </td>
  370. </tr>
  371. <tr>
  372. <td class="parameter_name"><p>size</p></td>
  373. <td class="parameter_description">
  374. <p></p>
  375. <p>Inferred from <code class="code">data</code></p>
  376. </td>
  377. </tr>
  378. </table></div>
  379. </div>
  380. </div>
  381. <div class="refsect2">
  382. <a name="id-1.1.6.3.14"></a><h3>bytes:new</h3>
  383. <div class="informalexample"><pre class="programlisting">(define-values (%return) (bytes:new data))
  384. </pre></div>
  385. <p>Undocumented</p>
  386. <div class="refsect3">
  387. <a name="id-1.1.6.3.14.4"></a><h4>Parameters</h4>
  388. <div class="informaltable"><table>
  389. <tr>
  390. <td class="parameter_name"><p>data</p></td>
  391. <td class="parameter_description">
  392. <p></p>
  393. <p>Passed as <code class="code">data</code></p>
  394. </td>
  395. </tr>
  396. <tr>
  397. <td class="parameter_name"><p>size</p></td>
  398. <td class="parameter_description">
  399. <p></p>
  400. <p>Inferred from <code class="code">data</code></p>
  401. </td>
  402. </tr>
  403. </table></div>
  404. </div>
  405. </div>
  406. </div>
  407. </div>
  408. <div class="footer">
  409. <hr>Generated by GTK-Doc V1.33.1</div>
  410. </body>
  411. </html>