提交 cd6ead91 authored 作者: Anthony Minessale's avatar Anthony Minessale

add libks (kitchen sink) as a new lib to collect random cool cross platform BSD licensed C funcs

上级 cf480c8b
PWD=$(shell pwd)
INCS=-I$(PWD)/src/include
DEBUG=-g -ggdb
BASE_FLAGS=$(INCS) $(DEBUG) -I$(LIBEDIT_DIR)/src/ -fPIC
PICKY=-O2
CFLAGS=$(BASE_FLAGS) $(PICKY)
CXXFLAGS=$(BASE_FLAGS)
MYLIB=libks.a
LIBS=-lncurses -lks -lpthread -lm
LDFLAGS=-L.
OBJS=src/ks.o src/ks_threadmutex.o src/ks_config.o src/ks_json.o src/ks_buffer.o src/mpool.o src/table.o src/table_util.o src/simclist.o
SRC=src/ks.c src/ks_json.c src/ks_threadmutex.c src/ks_config.c src/ks_json.c src/ks_buffer.c src/mpool.c src/table.c src/table_util.c src/simclist.c
HEADERS=src/include/ks_config.h src/include/ks.h src/include/ks_threadmutex.h src/include/ks_json.h src/include/ks_buffer.h src/include/mpool.h src/include/mpool_loc.h src/include/table.h src/include/table_loc.h src/include/simclist.h
SOLINK=-shared -Xlinker -x
all: $(MYLIB)
$(MYLIB): $(OBJS) $(HEADERS) $(SRC)
ar rcs $(MYLIB) $(OBJS)
ranlib $(MYLIB)
%.o: %.c $(HEADERS)
$(CC) $(CC_CFLAGS) $(CFLAGS) -c $< -o $@
clean:
rm -f *.o src/*.o libks.a *~ src/*~ src/include/*~
const char *cc = ".========================================================================================================.\n| ____ _____ ____ _ ____ _ _ _____ |\n| / ___|___ _ __ ___ ___ |_ _|__ / ___| |_ _ ___ / ___|___ _ __ ( ) |___ / |\n| | | / _ \\| '_ ` _ \\ / _ \\ | |/ _ \\ | | | | | | |/ _ \\ | / _ \\| '_ \\ |/| | |_ \\ |\n| | |__| (_) | | | | | | __/ | | (_) | | |___| | |_| | __/ |__| (_) | | | | | |___) | |\n| \\____\\___/|_| |_| |_|\\___| |_|\\___/ \\____|_|\\__,_|\\___|\\____\\___/|_| |_| |_|____/ |\n| |\n| ____ _ _ _ _ ____ _ |\n| / ___| |__ (_) ___ __ _ __ _ ___ | | | / ___| / \\ |\n| | | | '_ \\| |/ __/ _` |/ _` |/ _ \\ | | | \\___ \\ / _ \\ |\n| | |___| | | | | (_| (_| | (_| | (_) | | |_| |___) / ___ \\ |\n| \\____|_| |_|_|\\___\\__,_|\\__, |\\___( ) \\___/|____/_/ \\_\\ |\n| |___/ |/ |\n| _ _ __ _ _ ___ _ _ ____ ___ _ _____ |\n| / \\ _ _ __ _ _ _ ___| |_ / /_ | |_| |__ ( _ )| |_| |__ |___ \\ / _ \\/ |___ / |\n| / _ \\| | | |/ _` | | | / __| __| | '_ \\| __| '_ \\ _____ / _ \\| __| '_ \\ __) | | | | | |_ \\ |\n| / ___ \\ |_| | (_| | |_| \\__ \\ |_ | (_) | |_| | | | |_____| | (_) | |_| | | | / __/| |_| | |___) | |\n| /_/ \\_\\__,_|\\__, |\\__,_|___/\\__| \\___/ \\__|_| |_| \\___/ \\__|_| |_| |_____|\\___/|_|____/ |\n| |___/ |\n| _ |\n| __ ____ ____ __ ___| |_ _ ___ ___ ___ _ __ ___ ___ _ __ ___ |\n| \\ \\ /\\ / /\\ \\ /\\ / /\\ \\ /\\ / / / __| | | | |/ _ \\/ __/ _ \\| '_ \\ / __/ _ \\| '_ ` _ \\ |\n| \\ V V / \\ V V / \\ V V / _ | (__| | |_| | __/ (_| (_) | | | | _ | (_| (_) | | | | | | |\n| \\_/\\_/ \\_/\\_/ \\_/\\_/ (_) \\___|_|\\__,_|\\___|\\___\\___/|_| |_| (_) \\___\\___/|_| |_| |_| |\n| |\n.========================================================================================================.\n";
差异被折叠。
/*
* Copyright (c) 2010-2012, Anthony Minessale II
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ks.h"
#ifndef KS_BUFFER_H
#define KS_BUFFER_H
/**
* @defgroup ks_buffer Buffer Routines
* @ingroup buffer
* The purpose of this module is to make a plain buffering interface that can be used for read/write buffers
* throughout the application.
* @{
*/
struct ks_buffer;
typedef struct ks_buffer ks_buffer_t;
/*! \brief Allocate a new dynamic ks_buffer
* \param buffer returned pointer to the new buffer
* \param blocksize length to realloc by as data is added
* \param start_len ammount of memory to reserve initially
* \param max_len length the buffer is allowed to grow to
* \return status
*/
KS_DECLARE(ks_status_t) ks_buffer_create(ks_buffer_t **buffer, ks_size_t blocksize, ks_size_t start_len, ks_size_t max_len);
/*! \brief Get the length of a ks_buffer_t
* \param buffer any buffer of type ks_buffer_t
* \return int size of the buffer.
*/
KS_DECLARE(ks_size_t) ks_buffer_len(ks_buffer_t *buffer);
/*! \brief Get the freespace of a ks_buffer_t
* \param buffer any buffer of type ks_buffer_t
* \return int freespace in the buffer.
*/
KS_DECLARE(ks_size_t) ks_buffer_freespace(ks_buffer_t *buffer);
/*! \brief Get the in use amount of a ks_buffer_t
* \param buffer any buffer of type ks_buffer_t
* \return int ammount of buffer curently in use
*/
KS_DECLARE(ks_size_t) ks_buffer_inuse(ks_buffer_t *buffer);
/*! \brief Read data from a ks_buffer_t up to the ammount of datalen if it is available. Remove read data from buffer.
* \param buffer any buffer of type ks_buffer_t
* \param data pointer to the read data to be returned
* \param datalen amount of data to be returned
* \return int ammount of data actually read
*/
KS_DECLARE(ks_size_t) ks_buffer_read(ks_buffer_t *buffer, void *data, ks_size_t datalen);
KS_DECLARE(ks_size_t) ks_buffer_read_packet(ks_buffer_t *buffer, void *data, ks_size_t maxlen);
KS_DECLARE(ks_size_t) ks_buffer_packet_count(ks_buffer_t *buffer);
/*! \brief Read data endlessly from a ks_buffer_t
* \param buffer any buffer of type ks_buffer_t
* \param data pointer to the read data to be returned
* \param datalen amount of data to be returned
* \return int ammount of data actually read
* \note Once you have read all the data from the buffer it will loop around.
*/
KS_DECLARE(ks_size_t) ks_buffer_read_loop(ks_buffer_t *buffer, void *data, ks_size_t datalen);
/*! \brief Assign a number of loops to read
* \param buffer any buffer of type ks_buffer_t
* \param loops the number of loops (-1 for infinite)
*/
KS_DECLARE(void) ks_buffer_set_loops(ks_buffer_t *buffer, int32_t loops);
/*! \brief Write data into a ks_buffer_t up to the length of datalen
* \param buffer any buffer of type ks_buffer_t
* \param data pointer to the data to be written
* \param datalen amount of data to be written
* \return int amount of buffer used after the write, or 0 if no space available
*/
KS_DECLARE(ks_size_t) ks_buffer_write(ks_buffer_t *buffer, const void *data, ks_size_t datalen);
/*! \brief Remove data from the buffer
* \param buffer any buffer of type ks_buffer_t
* \param datalen amount of data to be removed
* \return int size of buffer, or 0 if unable to toss that much data
*/
KS_DECLARE(ks_size_t) ks_buffer_toss(ks_buffer_t *buffer, ks_size_t datalen);
/*! \brief Remove all data from the buffer
* \param buffer any buffer of type ks_buffer_t
*/
KS_DECLARE(void) ks_buffer_zero(ks_buffer_t *buffer);
/*! \brief Destroy the buffer
* \param buffer buffer to destroy
* \note only neccessary on dynamic buffers (noop on pooled ones)
*/
KS_DECLARE(void) ks_buffer_destroy(ks_buffer_t **buffer);
/*! \brief Seek to offset from the beginning of the buffer
* \param buffer buffer to seek
* \param datalen offset in bytes
* \return new position
*/
KS_DECLARE(ks_size_t) ks_buffer_seek(ks_buffer_t *buffer, ks_size_t datalen);
/** @} */
KS_DECLARE(ks_size_t) ks_buffer_zwrite(ks_buffer_t *buffer, const void *data, ks_size_t datalen);
#endif
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/
/*
* Copyright (c) 2007-2012, Anthony Minessale II
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the original author; nor the names of any contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* @defgroup config Config File Parser
* @ingroup config
* This module implements a basic interface and file format parser
*
* <pre>
*
* EXAMPLE
*
* [category1]
* var1 => val1
* var2 => val2
* \# lines that begin with \# are comments
* \#var3 => val3
* </pre>
* @{
*/
#ifndef KS_CONFIG_H
#define KS_CONFIG_H
#include "ks.h"
#ifdef __cplusplus
extern "C" {
#endif /* defined(__cplusplus) */
#define KS_URL_SEPARATOR "://"
#ifdef WIN32
#define KS_PATH_SEPARATOR "\\"
#ifndef KS_CONFIG_DIR
#define KS_CONFIG_DIR "c:\\openks"
#endif
#define ks_is_file_path(file) (*(file +1) == ':' || *file == '/' || strstr(file, SWITCH_URL_SEPARATOR))
#else
#define KS_PATH_SEPARATOR "/"
#ifndef KS_CONFIG_DIR
#define KS_CONFIG_DIR "/etc/openks"
#endif
#define ks_is_file_path(file) ((*file == '/') || strstr(file, SWITCH_URL_SEPARATOR))
#endif
/*!
\brief Evaluate the truthfullness of a string expression
\param expr a string expression
\return true or false
*/
#define ks_true(expr)\
(expr && ( !strcasecmp(expr, "yes") ||\
!strcasecmp(expr, "on") ||\
!strcasecmp(expr, "true") ||\
!strcasecmp(expr, "enabled") ||\
!strcasecmp(expr, "active") ||\
!strcasecmp(expr, "allow") ||\
atoi(expr))) ? 1 : 0
/*!
\brief Evaluate the falsefullness of a string expression
\param expr a string expression
\return true or false
*/
#define ks_false(expr)\
(expr && ( !strcasecmp(expr, "no") ||\
!strcasecmp(expr, "off") ||\
!strcasecmp(expr, "false") ||\
!strcasecmp(expr, "disabled") ||\
!strcasecmp(expr, "inactive") ||\
!strcasecmp(expr, "disallow") ||\
!atoi(expr))) ? 1 : 0
typedef struct ks_config ks_config_t;
/*! \brief A simple file handle representing an open configuration file **/
struct ks_config {
/*! FILE stream buffer to the opened file */
FILE *file;
/*! path to the file */
char path[512];
/*! current category */
char category[256];
/*! current section */
char section[256];
/*! buffer of current line being read */
char buf[1024];
/*! current line number in file */
int lineno;
/*! current category number in file */
int catno;
/*! current section number in file */
int sectno;
int lockto;
};
/*!
\brief Open a configuration file
\param cfg (ks_config_t *) config handle to use
\param file_path path to the file
\return 1 (true) on success 0 (false) on failure
*/
KS_DECLARE(int) ks_config_open_file(ks_config_t * cfg, const char *file_path);
/*!
\brief Close a previously opened configuration file
\param cfg (ks_config_t *) config handle to use
*/
KS_DECLARE(void) ks_config_close_file(ks_config_t * cfg);
/*!
\brief Retrieve next name/value pair from configuration file
\param cfg (ks_config_t *) config handle to use
\param var pointer to aim at the new variable name
\param val pointer to aim at the new value
*/
KS_DECLARE(int) ks_config_next_pair(ks_config_t * cfg, char **var, char **val);
/*!
\brief Retrieve the CAS bits from a configuration string value
\param strvalue pointer to the configuration string value (expected to be in format whatever:xxxx)
\param outbits pointer to aim at the CAS bits
*/
KS_DECLARE(int) ks_config_get_cas_bits(char *strvalue, unsigned char *outbits);
/** @} */
#ifdef __cplusplus
}
#endif /* defined(__cplusplus) */
#endif /* defined(KS_CONFIG_H) */
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/
/*
Copyright (c) 2009 Dave Gamble
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "ks.h"
#ifndef cJSON__h
#define cJSON__h
#ifdef __cplusplus
extern "C"
{
#endif
/* cJSON Types: */
#define cJSON_False 0
#define cJSON_True 1
#define cJSON_NULL 2
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6
#define cJSON_IsReference 256
/* The cJSON structure: */
typedef struct cJSON {
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
int type; /* The type of the item, as above. */
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;
/* Supply malloc, realloc and free functions to cJSON */
KS_DECLARE(void) cJSON_InitHooks(cJSON_Hooks* hooks);
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
KS_DECLARE(cJSON *)cJSON_Parse(const char *value);
/* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
KS_DECLARE(char *)cJSON_Print(cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
KS_DECLARE(char *)cJSON_PrintUnformatted(cJSON *item);
/* Delete a cJSON entity and all subentities. */
KS_DECLARE(void) cJSON_Delete(cJSON *c);
/* Returns the number of items in an array (or object). */
KS_DECLARE(int) cJSON_GetArraySize(cJSON *array);
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
KS_DECLARE(cJSON *)cJSON_GetArrayItem(cJSON *array,int item);
/* Get item "string" from object. Case insensitive. */
KS_DECLARE(cJSON *)cJSON_GetObjectItem(cJSON *object,const char *string);
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
KS_DECLARE(const char *)cJSON_GetErrorPtr();
/* These calls create a cJSON item of the appropriate type. */
KS_DECLARE(cJSON *)cJSON_CreateNull();
KS_DECLARE(cJSON *)cJSON_CreateTrue();
KS_DECLARE(cJSON *)cJSON_CreateFalse();
KS_DECLARE(cJSON *)cJSON_CreateBool(int b);
KS_DECLARE(cJSON *)cJSON_CreateNumber(double num);
KS_DECLARE(cJSON *)cJSON_CreateString(const char *string);
KS_DECLARE(cJSON *)cJSON_CreateArray();
KS_DECLARE(cJSON *)cJSON_CreateObject();
/* These utilities create an Array of count items. */
KS_DECLARE(cJSON *)cJSON_CreateIntArray(int *numbers,int count);
KS_DECLARE(cJSON *)cJSON_CreateFloatArray(float *numbers,int count);
KS_DECLARE(cJSON *)cJSON_CreateDoubleArray(double *numbers,int count);
KS_DECLARE(cJSON *)cJSON_CreateStringArray(const char **strings,int count);
/* Append item to the specified array/object. */
KS_DECLARE(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
KS_DECLARE(void) cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
KS_DECLARE(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
KS_DECLARE(void) cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
/* Remove/Detatch items from Arrays/Objects. */
KS_DECLARE(cJSON *)cJSON_DetachItemFromArray(cJSON *array,int which);
KS_DECLARE(void) cJSON_DeleteItemFromArray(cJSON *array,int which);
KS_DECLARE(cJSON *)cJSON_DetachItemFromObject(cJSON *object,const char *string);
KS_DECLARE(void) cJSON_DeleteItemFromObject(cJSON *object,const char *string);
/* Update array items. */
KS_DECLARE(void) cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
KS_DECLARE(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
#ifdef __cplusplus
}
#endif
#endif
/*
* Cross Platform Thread/Mutex abstraction
* Copyright(C) 2007 Michael Jerris
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so.
*
* This work is provided under this license on an "as is" basis, without warranty of any kind,
* either expressed or implied, including, without limitation, warranties that the covered code
* is free of defects, merchantable, fit for a particular purpose or non-infringing. The entire
* risk as to the quality and performance of the covered code is with you. Should any covered
* code prove defective in any respect, you (not the initial developer or any other contributor)
* assume the cost of any necessary servicing, repair or correction. This disclaimer of warranty
* constitutes an essential part of this license. No use of any covered code is authorized hereunder
* except under this disclaimer.
*
*/
#ifndef _KS_THREADMUTEX_H
#define _KS_THREADMUTEX_H
#include "ks.h"
#ifdef __cplusplus
extern "C" {
#endif /* defined(__cplusplus) */
typedef struct ks_mutex ks_mutex_t;
typedef struct ks_thread ks_thread_t;
typedef void *(*ks_thread_function_t) (ks_thread_t *, void *);
KS_DECLARE(ks_status_t) ks_thread_create_detached(ks_thread_function_t func, void *data);
ks_status_t ks_thread_create_detached_ex(ks_thread_function_t func, void *data, size_t stack_size);
void ks_thread_override_default_stacksize(size_t size);
KS_DECLARE(ks_status_t) ks_mutex_create(ks_mutex_t **mutex);
KS_DECLARE(ks_status_t) ks_mutex_destroy(ks_mutex_t **mutex);
KS_DECLARE(ks_status_t) ks_mutex_lock(ks_mutex_t *mutex);
KS_DECLARE(ks_status_t) ks_mutex_trylock(ks_mutex_t *mutex);
KS_DECLARE(ks_status_t) ks_mutex_unlock(ks_mutex_t *mutex);
#ifdef __cplusplus
}
#endif /* defined(__cplusplus) */
#endif /* defined(_KS_THREADMUTEX_H) */
/* For Emacs:
* Local Variables:
* mode:c
* indent-tabs-mode:t
* tab-width:4
* c-basic-offset:4
* End:
* For VIM:
* vim:set softtabstop=4 shiftwidth=4 tabstop=4:
*/
差异被折叠。
/*
* Memory pool local defines.
*
* Copyright 1996 by Gray Watson.
*
* This file is part of the mpool package.
*
* Permission to use, copy, modify, and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all
* copies, and that the name of Gray Watson not be used in advertising
* or publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: mpool_loc.h,v 1.2 2005/05/20 20:08:54 gray Exp $
*/
#ifndef __MPOOL_LOC_H__
#define __MPOOL_LOC_H__
#define MPOOL_MAGIC 0xABACABA /* magic for struct */
#define BLOCK_MAGIC 0xB1B1007 /* magic for blocks */
#define FENCE_MAGIC0 (unsigned char)(0xFAU) /* 1st magic mem byte */
#define FENCE_MAGIC1 (unsigned char)(0xD3U) /* 2nd magic mem byte */
#define FENCE_SIZE 2 /* fence space */
#define MIN_ALLOCATION (sizeof(mpool_free_t)) /* min alloc */
#define MAX_FREE_SEARCH 10240 /* max size to search */
#define MAX_FREE_LIST_SEARCH 100 /* max looking for free mem */
/*
* bitflag tools for Variable and a Flag
*/
#define BIT_FLAG(x) (1 << (x))
#define BIT_SET(v,f) (v) |= (f)
#define BIT_CLEAR(v,f) (v) &= ~(f)
#define BIT_IS_SET(v,f) ((v) & (f))
#define BIT_TOGGLE(v,f) (v) ^= (f)
#define SET_POINTER(pnt, val) \
do { \
if ((pnt) != NULL) { \
(*(pnt)) = (val); \
} \
} while(0)
#define BLOCK_FLAG_USED BIT_FLAG(0) /* block is used */
#define BLOCK_FLAG_FREE BIT_FLAG(1) /* block is free */
#define DEFAULT_PAGE_MULT 16 /* pagesize = this * getpagesize*/
/* How many pages SIZE bytes resides in. We add in the block header. */
#define PAGES_IN_SIZE(mp_p, size) (((size) + sizeof(mpool_block_t) + \
(mp_p)->mp_page_size - 1) / \
(mp_p)->mp_page_size)
#define SIZE_OF_PAGES(mp_p, page_n) ((page_n) * (mp_p)->mp_page_size)
#define MAX_BITS 30 /* we only can allocate 1gb chunks */
#define MAX_BLOCK_USER_MEMORY(mp_p) ((mp_p)->mp_page_size - \
sizeof(mpool_block_t))
#define FIRST_ADDR_IN_BLOCK(block_p) (void *)((char *)(block_p) + \
sizeof(mpool_block_t))
#define MEMORY_IN_BLOCK(block_p) ((char *)(block_p)->mb_bounds_p - \
((char *)(block_p) + \
sizeof(mpool_block_t)))
typedef struct {
unsigned int mp_magic; /* magic number for struct */
unsigned int mp_flags; /* flags for the struct */
unsigned long mp_alloc_c; /* number of allocations */
unsigned long mp_user_alloc; /* user bytes allocated */
unsigned long mp_max_alloc; /* maximum user bytes allocated */
unsigned int mp_page_c; /* number of pages allocated */
unsigned int mp_max_pages; /* maximum number of pages to use */
unsigned int mp_page_size; /* page-size of our system */
int mp_fd; /* fd for /dev/zero if mmap-ing */
off_t mp_top; /* top of our allocations in fd */
mpool_log_func_t mp_log_func; /* log callback function */
void *mp_addr; /* current address for mmaping */
void *mp_min_p; /* min address in pool for checks */
void *mp_bounds_p; /* max address in pool for checks */
struct mpool_block_st *mp_first_p; /* first memory block we are using */
struct mpool_block_st *mp_last_p; /* last memory block we are using */
struct mpool_block_st *mp_free[MAX_BITS + 1]; /* free lists based on size */
unsigned int mp_magic2; /* upper magic for overwrite sanity */
} mpool_t;
/* for debuggers to be able to interrogate the generic type in the .h file */
typedef mpool_t mpool_ext_t;
/*
* Block header structure. This structure *MUST* be long-word
* aligned.
*/
typedef struct mpool_block_st {
unsigned int mb_magic; /* magic number for block header */
void *mb_bounds_p; /* block boundary location */
struct mpool_block_st *mb_next_p; /* linked list next pointer */
unsigned int mb_magic2; /* upper magic for overwrite sanity */
} mpool_block_t;
/*
* Free list structure.
*/
typedef struct {
void *mf_next_p; /* pointer to the next free address */
unsigned long mf_size; /* size of the free block */
} mpool_free_t;
#endif /* ! __MPOOL_LOC_H__ */
差异被折叠。
差异被折叠。
/*
* local defines for the table module
*
* Copyright 2000 by Gray Watson.
*
* This file is part of the table package.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose and without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies,
* and that the name of Gray Watson not be used in advertising or
* publicity pertaining to distribution of the document or software
* without specific, written prior permission.
*
* Gray Watson makes no representations about the suitability of the
* software described herein for any purpose. It is provided "as is"
* without express or implied warranty.
*
* The author may be reached via http://256.com/gray/
*
* $Id: table_loc.h,v 1.11 2000/03/09 03:30:42 gray Exp $
*/
#ifndef __TABLE_LOC_H__
#define __TABLE_LOC_H__
#ifndef unix
#define NO_MMAP
#endif
#ifndef BITSPERBYTE
#define BITSPERBYTE 8
#endif
#ifndef BITS
#define BITS(type) (BITSPERBYTE * (int)sizeof(type))
#endif
#define TABLE_MAGIC 0xBADF00D /* very magic magicness */
#define LINEAR_MAGIC 0xAD00D00 /* magic value for linear struct */
#define DEFAULT_SIZE 1024 /* default table size */
#define MAX_ALIGNMENT 128 /* max alignment value */
/*
* Maximum number of splits. This should mean that these routines can
* handle at least 2^128 different values (that's _quite_ a few). And
* then you can always increase the value.
*/
#define MAX_QSORT_SPLITS 128
/*
* Maximum number of entries that must be in list for it to be
* partitioned. If there are fewer elements then just do our
* insertion sort.
*/
#define MAX_QSORT_MANY 8
/*
* Macros.
*/
/* returns 1 when we should grow or shrink the table */
#define SHOULD_TABLE_GROW(tab) ((tab)->ta_entry_n > (tab)->ta_bucket_n * 2)
#define SHOULD_TABLE_SHRINK(tab) ((tab)->ta_entry_n < (tab)->ta_bucket_n / 2)
/*
* void HASH_MIX
*
* DESCRIPTION:
*
* Mix 3 32-bit values reversibly. For every delta with one or two
* bits set, and the deltas of all three high bits or all three low
* bits, whether the original value of a,b,c is almost all zero or is
* uniformly distributed.
*
* If HASH_MIX() is run forward or backward, at least 32 bits in a,b,c
* have at least 1/4 probability of changing. If mix() is run
* forward, every bit of c will change between 1/3 and 2/3 of the
* time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
*
* HASH_MIX() takes 36 machine instructions, but only 18 cycles on a
* superscalar machine (like a Pentium or a Sparc). No faster mixer
* seems to work, that's the result of my brute-force search. There
* were about 2^68 hashes to choose from. I only tested about a
* billion of those.
*/
#define HASH_MIX(a, b, c) \
do { \
a -= b; a -= c; a ^= (c >> 13); \
b -= c; b -= a; b ^= (a << 8); \
c -= a; c -= b; c ^= (b >> 13); \
a -= b; a -= c; a ^= (c >> 12); \
b -= c; b -= a; b ^= (a << 16); \
c -= a; c -= b; c ^= (b >> 5); \
a -= b; a -= c; a ^= (c >> 3); \
b -= c; b -= a; b ^= (a << 10); \
c -= a; c -= b; c ^= (b >> 15); \
} while(0)
#define SET_POINTER(pnt, val) \
do { \
if ((pnt) != NULL) { \
(*(pnt)) = (val); \
} \
} while(0)
/*
* The following macros take care of the mmap case. When we are
* mmaping a table from a disk file, all of the pointers in the table
* structures are replaced with offsets into the file. The following
* macro, for each pointer, adds the starting address of the mmaped
* section onto each pointer/offset turning it back into a legitimate
* pointer.
*/
#ifdef NO_MMAP
#define TABLE_POINTER(table, type, pnt) (pnt)
#else
#define TABLE_POINTER(tab_p, type, pnt) \
((tab_p)->ta_mmap == NULL || (pnt) == NULL ? (pnt) : \
(type)((char *)((tab_p)->ta_mmap) + (long)(pnt)))
#endif
/*
* Macros to get at the key and the data pointers
*/
#define ENTRY_KEY_BUF(entry_p) ((entry_p)->te_key_buf)
#define ENTRY_DATA_BUF(tab_p, entry_p) \
(ENTRY_KEY_BUF(entry_p) + (entry_p)->te_key_size)
/*
* Table structures...
*/
/*
* HACK: this should be equiv as the table_entry_t without the key_buf
* char. We use this with the ENTRY_SIZE() macro above which solves
* the problem with the lack of the [0] GNU hack. We use the
* table_entry_t structure to better map the memory and make things
* faster.
*/
typedef struct table_shell_st {
unsigned int te_key_size; /* size of data */
unsigned int te_data_size; /* size of data */
struct table_shell_st *te_next_p; /* pointer to next in the list */
/* NOTE: this does not have the te_key_buf field here */
} table_shell_t;
/*
* Elements in the bucket linked-lists. The key[1] is the start of
* the key with the rest of the key and all of the data information
* packed in memory directly after the end of this structure.
*
* NOTE: if this structure is changed, the table_shell_t must be
* changed to match.
*/
typedef struct table_entry_st {
unsigned int te_key_size; /* size of data */
unsigned int te_data_size; /* size of data */
struct table_entry_st *te_next_p; /* pointer to next in the list */
unsigned char te_key_buf[1]; /* 1st byte of key buf */
} table_entry_t;
/* external structure for debuggers be able to see void */
typedef table_entry_t table_entry_ext_t;
/* main table structure */
typedef struct table_st {
unsigned int ta_magic; /* magic number */
unsigned int ta_flags; /* table's flags defined in table.h */
unsigned int ta_bucket_n; /* num of buckets, should be 2^X */
unsigned int ta_entry_n; /* num of entries in all buckets */
unsigned int ta_data_align; /* data alignment value */
table_entry_t **ta_buckets; /* array of linked lists */
table_linear_t ta_linear; /* linear tracking */
struct table_st *ta_mmap; /* mmaped table */
unsigned long ta_file_size; /* size of on-disk space */
void *ta_mem_pool; /* pointer to some memory pool */
table_mem_alloc_t ta_alloc_func; /* memory allocation function */
table_mem_resize_t ta_resize_func; /* memory resize function */
table_mem_free_t ta_free_func; /* memory free function */
} table_t;
/* external table structure for debuggers */
typedef table_t table_ext_t;
/* local comparison functions */
typedef int (*compare_t)(const void *element1_p, const void *element2_p,
table_compare_t user_compare,
const table_t *table_p, int *err_bp);
/*
* to map error to string
*/
typedef struct {
int es_error; /* error number */
char *es_string; /* assocaited string */
} error_str_t;
static error_str_t errors[] = {
{ TABLE_ERROR_NONE, "no error" },
{ TABLE_ERROR_PNT, "invalid table pointer" },
{ TABLE_ERROR_ARG_NULL, "buffer argument is null" },
{ TABLE_ERROR_SIZE, "incorrect size argument" },
{ TABLE_ERROR_OVERWRITE, "key exists and no overwrite" },
{ TABLE_ERROR_NOT_FOUND, "key does not exist" },
{ TABLE_ERROR_ALLOC, "error allocating memory" },
{ TABLE_ERROR_LINEAR, "linear access not in progress" },
{ TABLE_ERROR_OPEN, "could not open file" },
{ TABLE_ERROR_SEEK, "could not seek to position in file" },
{ TABLE_ERROR_READ, "could not read from file" },
{ TABLE_ERROR_WRITE, "could not write to file" },
{ TABLE_ERROR_MMAP_NONE, "no mmap support compiled in library" },
{ TABLE_ERROR_MMAP, "could not mmap the file" },
{ TABLE_ERROR_MMAP_OP, "operation not valid on mmap files" },
{ TABLE_ERROR_EMPTY, "table is empty" },
{ TABLE_ERROR_NOT_EMPTY, "table contains data" },
{ TABLE_ERROR_ALIGNMENT, "invalid alignment value" },
{ TABLE_ERROR_COMPARE, "problems with internal comparison" },
{ TABLE_ERROR_FREE, "memory free error" },
{ 0 }
};
#define INVALID_ERROR "invalid error code"
#endif /* ! __TABLE_LOC_H__ */
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
差异被折叠。
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论