From b4d1f44af16809e70ac5800c7539875832757679 Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Sat, 11 Oct 2014 13:03:21 -0300 Subject: [PATCH] Move strbuf implementation to shared/ Just move the strbuf-like implementation to shared/. No renames were made yet to avoid cluttering the diff. It will come in a separate patch. --- Makefile.am | 2 + libkmod/libkmod-index.c | 112 +----------------------------------- shared/strbuf.c | 123 ++++++++++++++++++++++++++++++++++++++++ shared/strbuf.h | 30 ++++++++++ 4 files changed, 156 insertions(+), 111 deletions(-) create mode 100644 shared/strbuf.c create mode 100644 shared/strbuf.h diff --git a/Makefile.am b/Makefile.am index 39b8e2e..e2c5301 100644 --- a/Makefile.am +++ b/Makefile.am @@ -50,6 +50,8 @@ shared_libshared_la_SOURCES = \ shared/array.h \ shared/hash.c \ shared/hash.h \ + shared/strbuf.c \ + shared/strbuf.h \ shared/util.c \ shared/util.h diff --git a/libkmod/libkmod-index.c b/libkmod/libkmod-index.c index e8e8032..878fcc5 100644 --- a/libkmod/libkmod-index.c +++ b/libkmod/libkmod-index.c @@ -28,6 +28,7 @@ #include #include +#include #include #include "libkmod-internal.h" @@ -133,100 +134,6 @@ static uint32_t read_long(FILE *in) return ntohl(l); } -/* - * Buffer abstract data type - * - * Used internally to store the current path during tree traversal. - * They help build wildcard key strings to pass to fnmatch(), - * as well as building values of matching keys. - */ -struct buffer { - char *bytes; - unsigned size; - unsigned used; -}; - -#define BUF_STEP (2048) -static bool buf_grow(struct buffer *buf, size_t newsize) -{ - void *tmp; - size_t sz; - - if (newsize % BUF_STEP == 0) - sz = newsize; - else - sz = ((newsize / BUF_STEP) + 1) * BUF_STEP; - - if (buf->size == sz) - return true; - - tmp = realloc(buf->bytes, sz); - if (sz > 0 && tmp == NULL) - return false; - buf->bytes = tmp; - buf->size = sz; - return true; -} - -static void buf_init(struct buffer *buf) -{ - buf->bytes = NULL; - buf->size = 0; - buf->used = 0; -} - -static void buf_release(struct buffer *buf) -{ - free(buf->bytes); -} - -/* Destroy buffer and return a copy as a C string */ -static char *buf_steal(struct buffer *buf) -{ - char *bytes; - - bytes = realloc(buf->bytes, buf->used + 1); - if (!bytes) { - free(buf->bytes); - return NULL; - } - bytes[buf->used] = '\0'; - return bytes; -} - -/* Return a C string owned by the buffer - (invalidated if the buffer is changed). - */ -static const char *buf_str(struct buffer *buf) -{ - if (!buf_grow(buf, buf->used + 1)) - return NULL; - buf->bytes[buf->used] = '\0'; - return buf->bytes; -} - -static bool buf_pushchar(struct buffer *buf, char ch) -{ - if (!buf_grow(buf, buf->used + 1)) - return false; - buf->bytes[buf->used] = ch; - buf->used++; - return true; -} - -static unsigned buf_pushchars(struct buffer *buf, const char *str) -{ - unsigned i = 0; - int ch; - - while ((ch = str[i])) { - buf_pushchar(buf, ch); - i++; - } - - return i; -} - static unsigned buf_freadchars(struct buffer *buf, FILE *in) { unsigned i = 0; @@ -241,23 +148,6 @@ static unsigned buf_freadchars(struct buffer *buf, FILE *in) return i; } -static void buf_popchar(struct buffer *buf) -{ - assert(buf->used > 0); - buf->used--; -} - -static void buf_popchars(struct buffer *buf, unsigned n) -{ - assert(buf->used >= n); - buf->used -= n; -} - -static void buf_clear(struct buffer *buf) -{ - buf->used = 0; -} - /* * Index file searching */ diff --git a/shared/strbuf.c b/shared/strbuf.c new file mode 100644 index 0000000..5974660 --- /dev/null +++ b/shared/strbuf.c @@ -0,0 +1,123 @@ +/* + * libkmod - interface to kernel module operations + * + * Copyright (C) 2011-2013 ProFUSION embedded systems + * Copyright (C) 2014 Intel Corporation. All rights reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * 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 St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include + +#include "util.h" +#include "strbuf.h" + +#define BUF_STEP (2048) + +static bool buf_grow(struct buffer *buf, size_t newsize) +{ + void *tmp; + size_t sz; + + if (newsize % BUF_STEP == 0) + sz = newsize; + else + sz = ((newsize / BUF_STEP) + 1) * BUF_STEP; + + if (buf->size == sz) + return true; + + tmp = realloc(buf->bytes, sz); + if (sz > 0 && tmp == NULL) + return false; + buf->bytes = tmp; + buf->size = sz; + return true; +} + +void buf_init(struct buffer *buf) +{ + buf->bytes = NULL; + buf->size = 0; + buf->used = 0; +} + +void buf_release(struct buffer *buf) +{ + free(buf->bytes); +} + +char *buf_steal(struct buffer *buf) +{ + char *bytes; + + bytes = realloc(buf->bytes, buf->used + 1); + if (!bytes) { + free(buf->bytes); + return NULL; + } + bytes[buf->used] = '\0'; + return bytes; +} + +const char *buf_str(struct buffer *buf) +{ + if (!buf_grow(buf, buf->used + 1)) + return NULL; + buf->bytes[buf->used] = '\0'; + return buf->bytes; +} + +bool buf_pushchar(struct buffer *buf, char ch) +{ + if (!buf_grow(buf, buf->used + 1)) + return false; + buf->bytes[buf->used] = ch; + buf->used++; + return true; +} + +unsigned buf_pushchars(struct buffer *buf, const char *str) +{ + unsigned i = 0; + int ch; + + while ((ch = str[i])) { + buf_pushchar(buf, ch); + i++; + } + + return i; +} + +void buf_popchar(struct buffer *buf) +{ + assert(buf->used > 0); + buf->used--; +} + +void buf_popchars(struct buffer *buf, unsigned n) +{ + assert(buf->used >= n); + buf->used -= n; +} + +void buf_clear(struct buffer *buf) +{ + buf->used = 0; +} + diff --git a/shared/strbuf.h b/shared/strbuf.h new file mode 100644 index 0000000..d3ac55b --- /dev/null +++ b/shared/strbuf.h @@ -0,0 +1,30 @@ +#pragma once + +#include + +/* + * Buffer abstract data type + */ +struct buffer { + char *bytes; + unsigned size; + unsigned used; +}; + +void buf_init(struct buffer *buf); +void buf_release(struct buffer *buf); +void buf_clear(struct buffer *buf); + +/* Destroy buffer and return a copy as a C string */ +char *buf_steal(struct buffer *buf); + +/* + * Return a C string owned by the buffer invalidated if the buffer is + * changed). + */ +const char *buf_str(struct buffer *buf); + +bool buf_pushchar(struct buffer *buf, char ch); +unsigned buf_pushchars(struct buffer *buf, const char *str); +void buf_popchar(struct buffer *buf); +void buf_popchars(struct buffer *buf, unsigned n);