susumu.yata
null+****@clear*****
Mon Nov 9 13:36:05 JST 2015
susumu.yata 2015-11-09 13:36:05 +0900 (Mon, 09 Nov 2015) New Revision: 042d0f91261de56a85b9e9e71875fe57a2cc6479 https://github.com/groonga/groonga/commit/042d0f91261de56a85b9e9e71875fe57a2cc6479 Message: grn_ts: move definitions of grn_ts_buf into lib/ts/ts_buf Added files: lib/ts/ts_buf.c lib/ts/ts_buf.h Modified files: lib/ts.c lib/ts/sources.am Modified: lib/ts.c (+1 -117) =================================================================== --- lib/ts.c 2015-11-09 13:11:40 +0900 (e183e50) +++ lib/ts.c 2015-11-09 13:36:05 +0900 (5410343) @@ -35,6 +35,7 @@ #include "grn_store.h" #include "grn_str.h" +#include "ts/ts_buf.h" #include "ts/ts_log.h" #include "ts/ts_str.h" @@ -45,123 +46,6 @@ enum { GRN_TS_BATCH_SIZE = 1024 }; /*------------------------------------------------------------- - * grn_ts_buf. - */ - -typedef struct { - void *ptr; /* The starting address. */ - size_t size; /* The size in bytes. */ - size_t pos; /* The current position for grn_ts_buf_write(). */ -} grn_ts_buf; - -/* grn_ts_buf_init() initializes a buffer. */ -static void -grn_ts_buf_init(grn_ctx *ctx, grn_ts_buf *buf) { - buf->ptr = NULL; - buf->size = 0; - buf->pos = 0; -} - -/* grn_ts_buf_open() creates a buffer. */ -/* -static grn_rc -grn_ts_buf_open(grn_ctx *ctx, grn_ts_buf **buf) { - grn_ts_buf *new_buf = GRN_MALLOCN(grn_ts_buf, 1); - if (!new_buf) { - GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_MALLOCN failed: %zu x 1", - sizeof(grn_ts_buf)); - } - grn_ts_buf_init(ctx, new_buf); - *buf = new_buf; - return GRN_SUCCESS; -} -*/ - -/* grn_ts_buf_fin() finalizes a buffer. */ -static void -grn_ts_buf_fin(grn_ctx *ctx, grn_ts_buf *buf) { - if (buf->ptr) { - GRN_FREE(buf->ptr); - } -} - -/* grn_ts_buf_close() destroys a buffer. */ -/* -static void -grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf) { - if (buf) { - grn_ts_buf_fin(ctx, buf); - } -} -*/ - -/* - * grn_ts_buf_reserve() reserves enough memory to store new_size bytes. - * Note that this function never shrinks a buffer and does nothing if new_size - * is not greater than the current size. - */ -static grn_rc -grn_ts_buf_reserve(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size) { - void *new_ptr; - size_t enough_size; - if (new_size <= buf->size) { - return GRN_SUCCESS; - } - enough_size = buf->size ? (buf->size << 1) : 1; - while (enough_size < new_size) { - enough_size <<= 1; - } - new_ptr = GRN_REALLOC(buf->ptr, enough_size); - if (!new_ptr) { - GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_REALLOC failed: %zu", - enough_size); - } - buf->ptr = new_ptr; - buf->size = enough_size; - return GRN_SUCCESS; -} - -/* grn_ts_buf_resize() resizes a buffer. */ -static grn_rc -grn_ts_buf_resize(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size) { - void *new_ptr; - if (new_size == buf->size) { - return GRN_SUCCESS; - } - if (!new_size) { - if (buf->ptr) { - GRN_FREE(buf->ptr); - buf->ptr = NULL; - buf->size = new_size; - } - return GRN_SUCCESS; - } - new_ptr = GRN_REALLOC(buf->ptr, new_size); - if (!new_ptr) { - GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_REALLOC failed: %zu", - new_size); - } - buf->ptr = new_ptr; - buf->size = new_size; - return GRN_SUCCESS; -} - -/* grn_ts_buf_write() appends data into a buffer. */ -static grn_rc -grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf, const void *ptr, size_t size) { - size_t new_pos = buf->pos + size; - if (new_pos > buf->size) { - grn_rc rc = grn_ts_buf_reserve(ctx, buf, new_pos); - if (rc != GRN_SUCCESS) { - return rc; - } - } - grn_memcpy((char *)buf->ptr + buf->pos, ptr, size); - buf->pos += size; - return GRN_SUCCESS; -} - -/*------------------------------------------------------------- * Built-in data kinds. */ Modified: lib/ts/sources.am (+2 -0) =================================================================== --- lib/ts/sources.am 2015-11-09 13:11:40 +0900 (5f9a51a) +++ lib/ts/sources.am 2015-11-09 13:36:05 +0900 (c51787a) @@ -1,4 +1,6 @@ libgrnts_la_SOURCES = \ + ts_buf.c \ + ts_buf.h \ ts_log.h \ ts_str.c \ ts_str.h Added: lib/ts/ts_buf.c (+126 -0) 100644 =================================================================== --- /dev/null +++ lib/ts/ts_buf.c 2015-11-09 13:36:05 +0900 (30b8aff) @@ -0,0 +1,126 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "ts_buf.h" + +#include "../grn_ctx_impl.h" + +#include "ts_log.h" + +void +grn_ts_buf_init(grn_ctx *ctx, grn_ts_buf *buf) +{ + buf->ptr = NULL; + buf->size = 0; + buf->pos = 0; +} + +/* +grn_rc +grn_ts_buf_open(grn_ctx *ctx, grn_ts_buf **buf) +{ + grn_ts_buf *new_buf = GRN_MALLOCN(grn_ts_buf, 1); + if (!new_buf) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_MALLOCN failed: %zu x 1", + sizeof(grn_ts_buf)); + } + grn_ts_buf_init(ctx, new_buf); + *buf = new_buf; + return GRN_SUCCESS; +} +*/ + +void +grn_ts_buf_fin(grn_ctx *ctx, grn_ts_buf *buf) +{ + if (buf->ptr) { + GRN_FREE(buf->ptr); + } +} + +/* +void +grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf) +{ + if (buf) { + grn_ts_buf_fin(ctx, buf); + } +} +*/ + +grn_rc +grn_ts_buf_reserve(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size) +{ + void *new_ptr; + size_t enough_size; + if (new_size <= buf->size) { + return GRN_SUCCESS; + } + enough_size = buf->size ? (buf->size << 1) : 1; + while (enough_size < new_size) { + enough_size <<= 1; + } + new_ptr = GRN_REALLOC(buf->ptr, enough_size); + if (!new_ptr) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_REALLOC failed: %zu", + enough_size); + } + buf->ptr = new_ptr; + buf->size = enough_size; + return GRN_SUCCESS; +} + +grn_rc +grn_ts_buf_resize(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size) +{ + void *new_ptr; + if (new_size == buf->size) { + return GRN_SUCCESS; + } + if (!new_size) { + if (buf->ptr) { + GRN_FREE(buf->ptr); + buf->ptr = NULL; + buf->size = new_size; + } + return GRN_SUCCESS; + } + new_ptr = GRN_REALLOC(buf->ptr, new_size); + if (!new_ptr) { + GRN_TS_ERR_RETURN(GRN_NO_MEMORY_AVAILABLE, "GRN_REALLOC failed: %zu", + new_size); + } + buf->ptr = new_ptr; + buf->size = new_size; + return GRN_SUCCESS; +} + +grn_rc +grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf, const void *ptr, size_t size) +{ + size_t new_pos = buf->pos + size; + if (new_pos > buf->size) { + grn_rc rc = grn_ts_buf_reserve(ctx, buf, new_pos); + if (rc != GRN_SUCCESS) { + return rc; + } + } + grn_memcpy((char *)buf->ptr + buf->pos, ptr, size); + buf->pos += size; + return GRN_SUCCESS; +} Added: lib/ts/ts_buf.h (+64 -0) 100644 =================================================================== --- /dev/null +++ lib/ts/ts_buf.h 2015-11-09 13:36:05 +0900 (04c675e) @@ -0,0 +1,64 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2015 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef GRN_TS_BUF_H +#define GRN_TS_BUF_H + +#include "../grn_ts.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + void *ptr; /* The starting address. */ + size_t size; /* The size in bytes. */ + size_t pos; /* The current position for grn_ts_buf_write(). */ +} grn_ts_buf; + +/* 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); + +/* grn_ts_buf_close() destroys a buffer. */ +/*void grn_ts_buf_close(grn_ctx *ctx, grn_ts_buf *buf);*/ + +/* + * grn_ts_buf_reserve() reserves enough memory to store new_size bytes. + * Note that this function never shrinks a buffer and does nothing if new_size + * is not greater than the current size. + */ +grn_rc grn_ts_buf_reserve(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size); + +/* grn_ts_buf_resize() resizes a buffer. */ +grn_rc grn_ts_buf_resize(grn_ctx *ctx, grn_ts_buf *buf, size_t new_size); + +/* grn_ts_buf_write() appends data into a buffer. */ +grn_rc grn_ts_buf_write(grn_ctx *ctx, grn_ts_buf *buf, + const void *ptr, size_t size); + +#ifdef __cplusplus +} +#endif + +#endif /* GRN_TS_BUF_H */ -------------- next part -------------- HTML����������������������������... 다운로드