buffer.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /**
  2. * @file buffer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * buffer([string data])
  33. *
  34. * Variables:
  35. * string (empty) - data in the buffer
  36. * string length - number of bytes in the buffer
  37. *
  38. * Description:
  39. * Implements an array of bytes which supports appending bytes and removing
  40. * bytes from the beginning. The buffer is implemented using chunks;
  41. * the time complexity of operations depends on the number of chunks affected,
  42. * and not on the actual number of bytes. Each append operation produces a single
  43. * chunk. In particular:
  44. *
  45. * Complexity of append and construction:
  46. * log(total number of chunks) + (time for copying data).
  47. * Complexity of consume:
  48. * log(total number of chunks) * (1 + (number of chunks in consumed range))
  49. * Complexity of referencing and unreferencing a range:
  50. * log(total number of chunks) * (1 + (number of chunks in referenced range))
  51. *
  52. * Synopsis:
  53. * buffer::append(string data)
  54. *
  55. * Description:
  56. * Appends the given data to the end of the buffer.
  57. *
  58. * Synopsis:
  59. * buffer::consume(string amount)
  60. *
  61. * Description:
  62. * Removes the specified number of bytes from the beginning of the buffer.
  63. * 'amount' must not be larger than the current length of the buffer.
  64. */
  65. #include <string.h>
  66. #include <limits.h>
  67. #include <misc/debug.h>
  68. #include <misc/balloc.h>
  69. #include <misc/compare.h>
  70. #include <misc/offset.h>
  71. #include <structure/SAvl.h>
  72. #include <ncd/module_common.h>
  73. #include <generated/blog_channel_ncd_buffer.h>
  74. struct chunk;
  75. #include "buffer_chunks_tree.h"
  76. #include <structure/SAvl_decl.h>
  77. struct buffer {
  78. struct instance *inst;
  79. ChunksTree chunks_tree;
  80. int refcnt;
  81. };
  82. struct chunk {
  83. struct buffer *buf;
  84. size_t offset;
  85. size_t length;
  86. ChunksTreeNode chunks_tree_node;
  87. int refcnt;
  88. char data[];
  89. };
  90. struct reference {
  91. struct chunk *first_chunk;
  92. size_t first_offset;
  93. size_t length;
  94. BRefTarget ref_target;
  95. };
  96. struct instance {
  97. NCDModuleInst *i;
  98. size_t offset;
  99. size_t total_length;
  100. struct buffer *buf;
  101. };
  102. #include "buffer_chunks_tree.h"
  103. #include <structure/SAvl_impl.h>
  104. static void instance_assert (struct instance *inst);
  105. static int instance_append (struct instance *inst, NCDValRef string);
  106. static void instance_consume (struct instance *inst, size_t amount);
  107. static struct buffer * buffer_init (struct instance *inst, NCDModuleInst *i);
  108. static void buffer_free (struct buffer *buf);
  109. static void buffer_detach (struct buffer *buf);
  110. static struct chunk * buffer_get_existing_chunk (struct buffer *buf, size_t offset);
  111. static struct chunk * chunk_init (struct instance *inst, size_t length);
  112. static void chunk_unref (struct chunk *c);
  113. static void chunk_assert (struct chunk *c);
  114. static struct reference * reference_init (struct instance *inst, size_t offset, size_t length, NCDValComposedStringResource *out_resource);
  115. static void reference_ref_target_func_release (BRefTarget *ref_target);
  116. static void reference_assert (struct reference *ref);
  117. static void reference_resource_func_getptr (void *user, size_t offset, const char **out_data, size_t *out_length);
  118. static void instance_assert (struct instance *inst)
  119. {
  120. ASSERT(inst->buf->inst == inst)
  121. }
  122. static int instance_append (struct instance *inst, NCDValRef string)
  123. {
  124. instance_assert(inst);
  125. ASSERT(NCDVal_IsString(string))
  126. size_t length = NCDVal_StringLength(string);
  127. // if string is empty do nothing, we can't make an empty chunk
  128. if (length == 0) {
  129. return 1;
  130. }
  131. // init chunk
  132. struct chunk *c = chunk_init(inst, length);
  133. if (!c) {
  134. return 0;
  135. }
  136. // copy data to chunk
  137. NCDVal_StringCopyOut(string, 0, length, c->data);
  138. return 1;
  139. }
  140. static void instance_consume (struct instance *inst, size_t amount)
  141. {
  142. instance_assert(inst);
  143. ASSERT(amount <= inst->total_length - inst->offset)
  144. // nothing do to if amount is zero
  145. if (amount == 0) {
  146. return;
  147. }
  148. // find chunk where the byte in the buffer resides
  149. struct chunk *c = buffer_get_existing_chunk(inst->buf, inst->offset);
  150. // increment buffer offset
  151. inst->offset += amount;
  152. // unreference chunks which no longer contain buffer contents
  153. while (c && c->offset + c->length <= inst->offset) {
  154. struct chunk *next_c = ChunksTree_GetNext(&inst->buf->chunks_tree, 0, c);
  155. chunk_unref(c);
  156. c = next_c;
  157. }
  158. }
  159. static struct buffer * buffer_init (struct instance *inst, NCDModuleInst *i)
  160. {
  161. ASSERT(inst)
  162. // allocate structure
  163. struct buffer *buf = BAlloc(sizeof(*buf));
  164. if (!buf) {
  165. ModuleLog(i, BLOG_ERROR, "BAlloc failed");
  166. return NULL;
  167. }
  168. // set instance pointer
  169. buf->inst = inst;
  170. // init chunks tree
  171. ChunksTree_Init(&buf->chunks_tree);
  172. // set refcnt to 0 (number of reference objects)
  173. buf->refcnt = 0;
  174. return buf;
  175. }
  176. static void buffer_free (struct buffer *buf)
  177. {
  178. ASSERT(!buf->inst)
  179. ASSERT(ChunksTree_IsEmpty(&buf->chunks_tree))
  180. ASSERT(buf->refcnt == 0)
  181. // free structure
  182. BFree(buf);
  183. }
  184. static void buffer_detach (struct buffer *buf)
  185. {
  186. ASSERT(buf->inst)
  187. struct instance *inst = buf->inst;
  188. // consume entire buffer to free any chunks that aren't referenced
  189. instance_consume(inst, inst->total_length - inst->offset);
  190. // clear instance pointer
  191. buf->inst = NULL;
  192. // free buffer if there are no more chunks
  193. if (ChunksTree_IsEmpty(&buf->chunks_tree)) {
  194. buffer_free(buf);
  195. }
  196. }
  197. static struct chunk * buffer_get_existing_chunk (struct buffer *buf, size_t offset)
  198. {
  199. struct chunk *c = ChunksTree_GetLastLesserEqual(&buf->chunks_tree, 0, offset);
  200. ASSERT(c)
  201. chunk_assert(c);
  202. ASSERT(offset >= c->offset)
  203. ASSERT(offset < c->offset + c->length)
  204. return c;
  205. }
  206. static struct chunk * chunk_init (struct instance *inst, size_t length)
  207. {
  208. instance_assert(inst);
  209. ASSERT(length > 0)
  210. struct buffer *buf = inst->buf;
  211. // make sure length is not too large
  212. if (length >= SIZE_MAX - inst->total_length) {
  213. ModuleLog(inst->i, BLOG_ERROR, "length overflow");
  214. return NULL;
  215. }
  216. // allocate structure
  217. bsize_t size = bsize_add(bsize_fromsize(sizeof(struct chunk)), bsize_fromsize(length));
  218. struct chunk *c = BAllocSize(size);
  219. if (!c) {
  220. ModuleLog(inst->i, BLOG_ERROR, "BAllocSize failed");
  221. return NULL;
  222. }
  223. // set some members
  224. c->buf = buf;
  225. c->offset = inst->total_length;
  226. c->length = length;
  227. // insert into chunks tree
  228. int res = ChunksTree_Insert(&buf->chunks_tree, 0, c, NULL);
  229. B_ASSERT_USE(res)
  230. // set reference count to 1 (referenced by buffer contents)
  231. c->refcnt = 1;
  232. // increment buffer length
  233. inst->total_length += length;
  234. chunk_assert(c);
  235. return c;
  236. }
  237. static void chunk_unref (struct chunk *c)
  238. {
  239. chunk_assert(c);
  240. // decrement reference count
  241. c->refcnt--;
  242. // if reference count is not yet zero, do nothing else
  243. if (c->refcnt > 0) {
  244. return;
  245. }
  246. // remove from chunks tree
  247. ChunksTree_Remove(&c->buf->chunks_tree, 0, c);
  248. // free structure
  249. BFree(c);
  250. }
  251. static void chunk_assert (struct chunk *c)
  252. {
  253. ASSERT(c->buf)
  254. ASSERT(c->length > 0)
  255. ASSERT(!c->buf->inst || c->offset <= c->buf->inst->total_length)
  256. ASSERT(!c->buf->inst || c->length <= c->buf->inst->total_length - c->offset)
  257. ASSERT(c->refcnt > 0)
  258. }
  259. static struct reference * reference_init (struct instance *inst, size_t offset, size_t length, NCDValComposedStringResource *out_resource)
  260. {
  261. instance_assert(inst);
  262. struct buffer *buf = inst->buf;
  263. ASSERT(offset >= inst->offset)
  264. ASSERT(offset <= inst->total_length)
  265. ASSERT(length <= inst->total_length - offset)
  266. ASSERT(length > 0)
  267. ASSERT(out_resource)
  268. // check buffer reference count. This ensures we can always increment the
  269. // chunk reference counts, below. We use (INT_MAX - 1) here because the buffer
  270. // itself can also own references to chunks.
  271. if (buf->refcnt == INT_MAX - 1) {
  272. ModuleLog(inst->i, BLOG_ERROR, "too many references");
  273. return NULL;
  274. }
  275. // allocate structure
  276. struct reference *ref = BAlloc(sizeof(*ref));
  277. if (!ref) {
  278. ModuleLog(inst->i, BLOG_ERROR, "BAlloc failed");
  279. return NULL;
  280. }
  281. // find chunk where the first byte of the interval resides
  282. struct chunk *c = buffer_get_existing_chunk(buf, offset);
  283. // set some members
  284. ref->first_chunk = c;
  285. ref->first_offset = offset - c->offset;
  286. ref->length = length;
  287. // increment buffer reference count
  288. buf->refcnt++;
  289. // reference chunks
  290. do {
  291. struct chunk *next_c = ChunksTree_GetNext(&buf->chunks_tree, 0, c);
  292. ASSERT(c->refcnt < INT_MAX)
  293. c->refcnt++;
  294. c = next_c;
  295. } while (c && c->offset < offset + length);
  296. // init reference target
  297. BRefTarget_Init(&ref->ref_target, reference_ref_target_func_release);
  298. // write resource
  299. out_resource->func_getptr = reference_resource_func_getptr;
  300. out_resource->user = ref;
  301. out_resource->ref_target = &ref->ref_target;
  302. reference_assert(ref);
  303. return ref;
  304. }
  305. static void reference_ref_target_func_release (BRefTarget *ref_target)
  306. {
  307. struct reference *ref = UPPER_OBJECT(ref_target, struct reference, ref_target);
  308. reference_assert(ref);
  309. struct buffer *buf = ref->first_chunk->buf;
  310. // compute offset
  311. size_t offset = ref->first_chunk->offset + ref->first_offset;
  312. // unreference chunks
  313. struct chunk *c = ref->first_chunk;
  314. do {
  315. struct chunk *next_c = ChunksTree_GetNext(&buf->chunks_tree, 0, c);
  316. chunk_unref(c);
  317. c = next_c;
  318. } while (c && c->offset < offset + ref->length);
  319. // decrement buffer reference count
  320. ASSERT(buf->refcnt > 0)
  321. buf->refcnt--;
  322. // free structure
  323. BFree(ref);
  324. // if the instance has died and there are no more chunks, free buffer
  325. if (!buf->inst && ChunksTree_IsEmpty(&buf->chunks_tree)) {
  326. buffer_free(buf);
  327. }
  328. }
  329. static void reference_assert (struct reference *ref)
  330. {
  331. ASSERT(ref->first_chunk)
  332. ASSERT(ref->first_offset < ref->first_chunk->length)
  333. ASSERT(ref->length > 0)
  334. chunk_assert(ref->first_chunk);
  335. }
  336. static void reference_resource_func_getptr (void *user, size_t offset, const char **out_data, size_t *out_length)
  337. {
  338. struct reference *ref = user;
  339. reference_assert(ref);
  340. ASSERT(offset < ref->length)
  341. ASSERT(out_data)
  342. ASSERT(out_length)
  343. // compute absolute offset of request
  344. size_t abs_offset = ref->first_chunk->offset + ref->first_offset + offset;
  345. // find chunk where the byte at the requested offset resides
  346. struct chunk *c = buffer_get_existing_chunk(ref->first_chunk->buf, abs_offset);
  347. // compute offset of this byte within the chunk
  348. size_t chunk_offset = abs_offset - c->offset;
  349. // return the data from this byte to the end of the chunk
  350. *out_data = c->data + chunk_offset;
  351. *out_length = c->length - chunk_offset;
  352. }
  353. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  354. {
  355. struct instance *o = vo;
  356. o->i = i;
  357. // pass instance pointer to methods
  358. NCDModuleInst_Backend_PassMemToMethods(i);
  359. // read arguments
  360. NCDValRef data_arg = NCDVal_NewInvalid();
  361. if (!NCDVal_ListRead(params->args, 0) &&
  362. !NCDVal_ListRead(params->args, 1, &data_arg)
  363. ) {
  364. ModuleLog(i, BLOG_ERROR, "wrong arity");
  365. goto fail0;
  366. }
  367. if (!NCDVal_IsInvalid(data_arg) && !NCDVal_IsString(data_arg)) {
  368. ModuleLog(i, BLOG_ERROR, "wrong type");
  369. goto fail0;
  370. }
  371. // set offset and total length
  372. o->offset = 0;
  373. o->total_length = 0;
  374. // allocate buffer
  375. o->buf = buffer_init(o, i);
  376. if (!o->buf) {
  377. goto fail0;
  378. }
  379. // append initial data
  380. if (!NCDVal_IsInvalid(data_arg)) {
  381. if (!instance_append(o, data_arg)) {
  382. goto fail1;
  383. }
  384. }
  385. // signal up
  386. NCDModuleInst_Backend_Up(i);
  387. return;
  388. fail1:
  389. o->buf->inst = NULL;
  390. buffer_free(o->buf);
  391. fail0:
  392. NCDModuleInst_Backend_DeadError(i);
  393. }
  394. static void func_die (void *vo)
  395. {
  396. struct instance *o = vo;
  397. instance_assert(o);
  398. // detach buffer from instance
  399. buffer_detach(o->buf);
  400. // die
  401. NCDModuleInst_Backend_Dead(o->i);
  402. }
  403. static int func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out)
  404. {
  405. struct instance *o = vo;
  406. instance_assert(o);
  407. if (name == NCD_STRING_EMPTY) {
  408. if (o->total_length - o->offset == 0) {
  409. *out = NCDVal_NewStringUninitialized(mem, 0);
  410. } else {
  411. NCDValComposedStringResource resource;
  412. struct reference *ref = reference_init(o, o->offset, o->total_length - o->offset, &resource);
  413. if (!ref) {
  414. goto fail;
  415. }
  416. *out = NCDVal_NewComposedString(mem, resource, 0, ref->length);
  417. BRefTarget_Deref(resource.ref_target);
  418. }
  419. return 1;
  420. }
  421. if (name == NCD_STRING_LENGTH) {
  422. *out = ncd_make_uintmax(mem, o->total_length - o->offset);
  423. return 1;
  424. }
  425. return 0;
  426. fail:
  427. *out = NCDVal_NewInvalid();
  428. return 1;
  429. }
  430. static void append_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  431. {
  432. // read arguments
  433. NCDValRef data_arg;
  434. if (!NCDVal_ListRead(params->args, 1, &data_arg)) {
  435. ModuleLog(i, BLOG_ERROR, "wrong arity");
  436. goto fail0;
  437. }
  438. if (!NCDVal_IsString(data_arg)) {
  439. ModuleLog(i, BLOG_ERROR, "wrong type");
  440. goto fail0;
  441. }
  442. // get instance
  443. struct instance *inst = params->method_user;
  444. // append
  445. if (!instance_append(inst, data_arg)) {
  446. ModuleLog(i, BLOG_ERROR, "instance_append failed");
  447. goto fail0;
  448. }
  449. // go up
  450. NCDModuleInst_Backend_Up(i);
  451. return;
  452. fail0:
  453. NCDModuleInst_Backend_DeadError(i);
  454. }
  455. static void consume_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  456. {
  457. // read arguments
  458. NCDValRef amount_arg;
  459. if (!NCDVal_ListRead(params->args, 1, &amount_arg)) {
  460. ModuleLog(i, BLOG_ERROR, "wrong arity");
  461. goto fail0;
  462. }
  463. // parse amount
  464. uintmax_t amount;
  465. if (!ncd_read_uintmax(amount_arg, &amount)) {
  466. ModuleLog(i, BLOG_ERROR, "wrong amount");
  467. goto fail0;
  468. }
  469. // get instance
  470. struct instance *inst = params->method_user;
  471. // check amount
  472. if (amount > inst->total_length - inst->offset) {
  473. ModuleLog(i, BLOG_ERROR, "amount is more than buffer length");
  474. goto fail0;
  475. }
  476. // consume
  477. instance_consume(inst, amount);
  478. // go up
  479. NCDModuleInst_Backend_Up(i);
  480. return;
  481. fail0:
  482. NCDModuleInst_Backend_DeadError(i);
  483. }
  484. static struct NCDModule modules[] = {
  485. {
  486. .type = "buffer",
  487. .func_new2 = func_new,
  488. .func_die = func_die,
  489. .func_getvar2 = func_getvar,
  490. .alloc_size = sizeof(struct instance),
  491. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  492. }, {
  493. .type = "buffer::append",
  494. .func_new2 = append_func_new,
  495. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  496. }, {
  497. .type = "buffer::consume",
  498. .func_new2 = consume_func_new,
  499. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  500. }, {
  501. .type = NULL
  502. }
  503. };
  504. const struct NCDModuleGroup ncdmodule_buffer = {
  505. .modules = modules
  506. };