susumu.yata
null+****@clear*****
Tue Nov 24 15:28:50 JST 2015
susumu.yata 2015-11-24 15:28:50 +0900 (Tue, 24 Nov 2015) New Revision: 26df00a11b5ad15faba8b579c6741e8afe07f872 https://github.com/groonga/groonga/commit/26df00a11b5ad15faba8b579c6741e8afe07f872 Message: grn_ts: add grn_ts_rbuf Modified files: lib/ts/ts_buf.c lib/ts/ts_buf.h Modified: lib/ts/ts_buf.c (+103 -0) =================================================================== --- lib/ts/ts_buf.c 2015-11-25 01:12:19 +0900 (840ee98) +++ lib/ts/ts_buf.c 2015-11-24 15:28:50 +0900 (0801db6) @@ -24,6 +24,10 @@ #include <string.h> +/*------------------------------------------------------------- + * grn_ts_buf + */ + void grn_ts_buf_init(grn_ctx *ctx, grn_ts_buf *buf) { @@ -139,3 +143,102 @@ grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf, const void *ptr, size_t size) buf->pos += size; return GRN_SUCCESS; } + +/*------------------------------------------------------------- + * grn_ts_rbuf + */ + +void +grn_ts_rbuf_init(grn_ctx *ctx, grn_ts_rbuf *rbuf) +{ + rbuf->recs = NULL; + rbuf->n_recs = 0; + rbuf->max_n_recs = 0; +} + +void +grn_ts_rbuf_fin(grn_ctx *ctx, grn_ts_rbuf *rbuf) +{ + if (rbuf->recs) { + GRN_FREE(rbuf->recs); + } +} + +grn_rc +grn_ts_rbuf_open(grn_ctx *ctx, grn_ts_rbuf **rbuf) +{ + grn_ts_rbuf *new_rbuf = GRN_MALLOCN(grn_ts_rbuf, 1); + if (!new_rbuf) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, + "GRN_MALLOCN failed: %" GRN_FMT_SIZE " x 1", + sizeof(grn_ts_rbuf)); + } + grn_ts_rbuf_init(ctx, new_rbuf); + *rbuf = new_rbuf; + return GRN_SUCCESS; +} + +void +grn_ts_rbuf_close(grn_ctx *ctx, grn_ts_rbuf *rbuf) +{ + if (rbuf) { + grn_ts_rbuf_fin(ctx, rbuf); + } +} + +grn_rc +grn_ts_rbuf_reserve(grn_ctx *ctx, grn_ts_rbuf *rbuf, size_t min_max_n_recs) +{ + size_t n_bytes, enough_max_n_recs; + grn_ts_record *new_recs; + if (min_max_n_recs <= rbuf->max_n_recs) { + return GRN_SUCCESS; + } + enough_max_n_recs = rbuf->max_n_recs ? (rbuf->max_n_recs << 1) : 1; + while (enough_max_n_recs < min_max_n_recs) { + if ((enough_max_n_recs << 1) < enough_max_n_recs) { + GRN_TS_ERR_RETURN(GRN_INVALID_ARGUMENT, + "size overflow: %" GRN_FMT_SIZE, + min_max_n_recs); + } + enough_max_n_recs <<= 1; + } + n_bytes = sizeof(grn_ts_record) * enough_max_n_recs; + new_recs = GRN_REALLOC(rbuf->recs, n_bytes); + if (!new_recs) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, + "GRN_REALLOC failed: %" GRN_FMT_SIZE, + n_bytes); + } + rbuf->recs = new_recs; + rbuf->max_n_recs = enough_max_n_recs; + return GRN_SUCCESS; +} + +grn_rc +grn_ts_rbuf_resize(grn_ctx *ctx, grn_ts_rbuf *rbuf, size_t new_max_n_recs) +{ + size_t n_bytes; + grn_ts_record *new_recs; + if (new_max_n_recs == rbuf->max_n_recs) { + return GRN_SUCCESS; + } + if (!new_max_n_recs) { + if (rbuf->recs) { + GRN_FREE(rbuf->recs); + rbuf->recs = NULL; + rbuf->max_n_recs = new_max_n_recs; + } + return GRN_SUCCESS; + } + n_bytes = sizeof(grn_ts_record) * new_max_n_recs; + new_recs = GRN_REALLOC(rbuf->recs, n_bytes); + if (!new_recs) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, + "GRN_REALLOC failed: %" GRN_FMT_SIZE, + new_max_n_recs); + } + rbuf->recs = new_recs; + rbuf->max_n_recs = new_max_n_recs; + return GRN_SUCCESS; +} Modified: lib/ts/ts_buf.h (+47 -4) =================================================================== --- lib/ts/ts_buf.h 2015-11-25 01:12:19 +0900 (4c66a00) +++ lib/ts/ts_buf.h 2015-11-24 15:28:50 +0900 (01f9851) @@ -27,6 +27,12 @@ extern "C" { #endif +/*------------------------------------------------------------- + * grn_ts_buf + */ + +/* grn_ts_buf works as a buffer for arbitrary data. */ + typedef struct { void *ptr; /* The starting address. */ size_t size; /* The size in bytes. */ @@ -36,14 +42,16 @@ typedef struct { /* grn_ts_buf_init() initializes a buffer. */ void grn_ts_buf_init(grn_ctx *ctx, grn_ts_buf *buf); -/* grn_ts_buf_open() creates a buffer. */ -/*grn_rc grn_ts_buf_open(grn_ctx *ctx, grn_ts_buf **buf);*/ - /* grn_ts_buf_fin() finalizes a buffer. */ void grn_ts_buf_fin(grn_ctx *ctx, grn_ts_buf *buf); +#if 0 +/* grn_ts_buf_open() creates a buffer. */ +grn_rc grn_ts_buf_open(grn_ctx *ctx, grn_ts_buf **buf); + /* grn_ts_buf_close() destroys a buffer. */ -/*void grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf);*/ +void grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf); +#endif /* * grn_ts_buf_reserve() reserves enough memory to store new_size bytes. @@ -63,6 +71,41 @@ grn_rc grn_ts_buf_resize(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size); grn_rc grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf, const void *ptr, size_t size); +/*------------------------------------------------------------- + * grn_ts_rbuf + */ + +/* grn_ts_rbuf works as a buffer for records. */ + +typedef struct { + grn_ts_record *recs; /* Pointer to records. */ + size_t n_recs; /* The number of records. */ + size_t max_n_recs; /* The maximum number of records. */ +} grn_ts_rbuf; + +/* grn_ts_rbuf_init() initializes a buffer. */ +void grn_ts_rbuf_init(grn_ctx *ctx, grn_ts_rbuf *rbuf); + +/* grn_ts_rbuf_fin() finalizes a buffer. */ +void grn_ts_rbuf_fin(grn_ctx *ctx, grn_ts_rbuf *rbuf); + +/* grn_ts_rbuf_open() creates a buffer. */ +/*grn_rc grn_ts_rbuf_open(grn_ctx *ctx, grn_ts_rbuf **rbuf);*/ + +/* grn_ts_rbuf_close() destroys a buffer. */ +/*void grn_ts_rbuf_close(grn_ctx *ctx, grn_ts_rbuf *rbuf);*/ + +/* + * grn_ts_rbuf_reserve() reserves enough memory to store `n_recs` records. + * Note that this function never shrinks a buffer and does nothing if `n_recs` + * is not greater than the `rbuf->max_n_recs`. + */ +grn_rc grn_ts_rbuf_reserve(grn_ctx *ctx, grn_ts_rbuf *rbuf, size_t n_recs); + +/* grn_ts_rbuf_resize() resizes a buffer. */ +grn_rc grn_ts_rbuf_resize(grn_ctx *ctx, grn_ts_rbuf *rbuf, + size_t new_max_n_recs); + #ifdef __cplusplus } #endif -------------- next part -------------- HTML����������������������������... 下載