merged sstrcat function

Mon, 14 Jul 2014 16:54:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 14 Jul 2014 16:54:10 +0200
changeset 182
998bf7c643b4
parent 181
1e9012ad8215 (diff)
parent 180
2185f19dcc45 (current diff)
child 183
6a694f8f0084

merged sstrcat function

ucx/Makefile file | annotate | diff | comparison | revisions
ucx/stack.c file | annotate | diff | comparison | revisions
ucx/stack.h file | annotate | diff | comparison | revisions
ucx/string.c file | annotate | diff | comparison | revisions
     1.1 --- a/ucx/Makefile	Mon Jul 14 13:51:02 2014 +0200
     1.2 +++ b/ucx/Makefile	Mon Jul 14 16:54:10 2014 +0200
     1.3 @@ -39,6 +39,7 @@
     1.4  SRC += allocator.c
     1.5  SRC += logging.c
     1.6  SRC += buffer.c
     1.7 +SRC += stack.c
     1.8  
     1.9  OBJ   = $(SRC:%.c=../build/release/ucx/%$(OBJ_EXT))
    1.10  OBJ_D = $(SRC:%.c=../build/debug/ucx/%$(OBJ_EXT))
     2.1 --- a/ucx/mempool.h	Mon Jul 14 13:51:02 2014 +0200
     2.2 +++ b/ucx/mempool.h	Mon Jul 14 16:54:10 2014 +0200
     2.3 @@ -121,7 +121,7 @@
     2.4  /**
     2.5   * Allocates a pooled memory array.
     2.6   * 
     2.7 - * The contents of the allocated memory is set to zero.
     2.8 + * The content of the allocated memory is set to zero.
     2.9   * 
    2.10   * @param pool the memory pool
    2.11   * @param nelem amount of elements to allocate
    2.12 @@ -142,7 +142,7 @@
    2.13   * @param pool the memory pool
    2.14   * @param ptr a pointer to the memory that shall be reallocated
    2.15   * @param n the new size of the memory
    2.16 - * @return a pointer to the the location of the memory
    2.17 + * @return a pointer to the new location of the memory
    2.18   * @see ucx_allocator_realloc()
    2.19   */
    2.20  void *ucx_mempool_realloc(UcxMempool *pool, void *ptr, size_t n);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/ucx/stack.c	Mon Jul 14 16:54:10 2014 +0200
     3.3 @@ -0,0 +1,108 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2014 Olaf Wintermann. All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +
    3.32 +#include "stack.h"
    3.33 +#include <string.h>
    3.34 +
    3.35 +UcxStack ucx_stack_new(char* space, size_t size) {
    3.36 +    UcxStack stack;
    3.37 +    stack.size = size;
    3.38 +    stack.space = stack.top = space;
    3.39 +    
    3.40 +    UcxAllocator alloc;
    3.41 +    alloc.pool = &stack;
    3.42 +    alloc.malloc = (ucx_allocator_malloc) ucx_stack_malloc;
    3.43 +    alloc.calloc = (ucx_allocator_calloc) ucx_stack_calloc;
    3.44 +    alloc.realloc = (ucx_allocator_realloc) ucx_stack_realloc;
    3.45 +    alloc.free = (ucx_allocator_free) ucx_stack_free;
    3.46 +    
    3.47 +    stack.allocator = alloc;
    3.48 +    
    3.49 +    return stack;
    3.50 +}
    3.51 +
    3.52 +void *ucx_stack_malloc(UcxStack *stack, size_t n) {
    3.53 +    n += n % sizeof(void*);
    3.54 +
    3.55 +    if (stack->top + n + sizeof(size_t) > stack->space + stack->size) {
    3.56 +        return NULL;
    3.57 +    } else {
    3.58 +        void *ptr = stack->top;
    3.59 +        
    3.60 +        *((size_t*) (stack->top + n)) = n;
    3.61 +        stack->top += n + sizeof(size_t);
    3.62 +        
    3.63 +        return ptr;
    3.64 +    }
    3.65 +}
    3.66 +
    3.67 +void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize) {
    3.68 +    void *mem = ucx_stack_malloc(stack, nelem*elsize);
    3.69 +    memset(mem, 0, nelem*elsize);
    3.70 +    return mem;
    3.71 +}
    3.72 +
    3.73 +void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n) {
    3.74 +    if (ptr == stack->top - sizeof(size_t) - *((size_t*) stack->top - 1)) {
    3.75 +
    3.76 +        stack->top = (char*)ptr + n;
    3.77 +        *((size_t*)stack->top) = n;
    3.78 +        stack->top += sizeof(size_t);
    3.79 +        
    3.80 +        return ptr;
    3.81 +    } else {
    3.82 +        size_t* sptr = (size_t*) (((char*) ptr)-sizeof(size_t));
    3.83 +        if (*sptr < n) {
    3.84 +            void *nptr = ucx_stack_malloc(stack, n);
    3.85 +            if (nptr) {
    3.86 +                memcpy(nptr, ptr, *sptr);
    3.87 +                ucx_stack_free(stack, ptr);
    3.88 +                return nptr;
    3.89 +            } else {
    3.90 +                return NULL;
    3.91 +            }
    3.92 +        } else {
    3.93 +            *sptr = n;
    3.94 +            return ptr;
    3.95 +        }
    3.96 +    }
    3.97 +}
    3.98 +
    3.99 +void ucx_stack_free(UcxStack *stack, void *ptr) {
   3.100 +    if (ptr == stack->top+sizeof(size_t)) {
   3.101 +        
   3.102 +    } else {
   3.103 +        
   3.104 +    }
   3.105 +}
   3.106 +
   3.107 +void ucx_stack_pop(UcxStack *stack, void *dest) {
   3.108 +}
   3.109 +
   3.110 +void ucx_stack_popn(UcxStack *stack, void *dest, size_t n) {
   3.111 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/ucx/stack.h	Mon Jul 14 16:54:10 2014 +0200
     4.3 @@ -0,0 +1,198 @@
     4.4 +/*
     4.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     4.6 + *
     4.7 + * Copyright 2014 Olaf Wintermann. All rights reserved.
     4.8 + *
     4.9 + * Redistribution and use in source and binary forms, with or without
    4.10 + * modification, are permitted provided that the following conditions are met:
    4.11 + *
    4.12 + *   1. Redistributions of source code must retain the above copyright
    4.13 + *      notice, this list of conditions and the following disclaimer.
    4.14 + *
    4.15 + *   2. Redistributions in binary form must reproduce the above copyright
    4.16 + *      notice, this list of conditions and the following disclaimer in the
    4.17 + *      documentation and/or other materials provided with the distribution.
    4.18 + *
    4.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    4.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    4.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    4.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    4.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    4.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    4.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    4.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    4.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    4.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    4.29 + * POSSIBILITY OF SUCH DAMAGE.
    4.30 + */
    4.31 +
    4.32 +/**
    4.33 + * @file stack.h
    4.34 + * 
    4.35 + * Default stack memory allocation implementation.
    4.36 + * 
    4.37 + * @author Mike Becker
    4.38 + * @author Olaf Wintermann
    4.39 + */
    4.40 +
    4.41 +#ifndef UCX_STACK_H
    4.42 +#define	UCX_STACK_H
    4.43 +
    4.44 +#include "ucx.h"
    4.45 +#include <stddef.h>
    4.46 +#include "allocator.h"
    4.47 +
    4.48 +#ifdef	__cplusplus
    4.49 +extern "C" {
    4.50 +#endif
    4.51 +
    4.52 +
    4.53 +/**
    4.54 + * UCX stack structure.
    4.55 + */
    4.56 +typedef struct {
    4.57 +    /** UcxAllocator based on this stack */
    4.58 +    UcxAllocator allocator;
    4.59 +    
    4.60 +    /** Stack size. */
    4.61 +    size_t size;
    4.62 +    
    4.63 +    /** Pointer to the bottom of the stack */
    4.64 +    char *space;
    4.65 +    
    4.66 +    /** Pointer to the top of the stack */
    4.67 +    char *top;
    4.68 +} UcxStack;
    4.69 +
    4.70 +/**
    4.71 + * Wraps memory in a new UcxStack structure.
    4.72 + * 
    4.73 + * @param space the memory area that shall be managed
    4.74 + * @param size size of the memory area
    4.75 + * @return a new UcxStack structure
    4.76 + */
    4.77 +UcxStack ucx_stack_new(char* space, size_t size);
    4.78 +
    4.79 +/**
    4.80 + * Allocates stack memory.
    4.81 + * 
    4.82 + * @param stack a pointer to the stack
    4.83 + * @param n amount of memory to allocate
    4.84 + * @return a pointer to the allocated memory
    4.85 + * @see ucx_allocator_malloc()
    4.86 + */
    4.87 +void *ucx_stack_malloc(UcxStack *stack, size_t n);
    4.88 +
    4.89 +/**
    4.90 + * Alias for #ucx_stack_malloc().
    4.91 + * @param stack a pointer to the stack
    4.92 + * @param n amount of memory to allocate
    4.93 + * @return a pointer to the allocated memory
    4.94 + * @see ucx_stack_malloc
    4.95 + */
    4.96 +#define ucx_stack_push(s, n) ucx_stack_malloc(s, n)
    4.97 +
    4.98 +/**
    4.99 + * Allocates an array of stack memory
   4.100 + * 
   4.101 + * The content of the allocated memory is set to zero.
   4.102 + * 
   4.103 + * @param stack a pointer to the stack
   4.104 + * @param nelem amount of elements to allocate
   4.105 + * @param elsize amount of memory per element
   4.106 + * @return a pointer to the allocated memory
   4.107 + * @see ucx_allocator_calloc()
   4.108 + */
   4.109 +void *ucx_stack_calloc(UcxStack *stack, size_t nelem, size_t elsize);
   4.110 +
   4.111 +/**
   4.112 + * Alias for #ucx_stack_calloc().
   4.113 + * 
   4.114 + * @param stack a pointer to the stack
   4.115 + * @param nelem amount of elements to allocate
   4.116 + * @param elsize amount of memory per element
   4.117 + * @return a pointer to the allocated memory
   4.118 + * @see ucx_stack_calloc
   4.119 + */
   4.120 +#define ucx_stack_pusharr(st,n,es) ucx_stack_calloc(st,n,es)
   4.121 +
   4.122 +/**
   4.123 + * Reallocates memory on the stack.
   4.124 + * 
   4.125 + * Shrinking memory is always safe. Extending memory can be very expensive. 
   4.126 + * 
   4.127 + * @param stack the stack
   4.128 + * @param ptr a pointer to the memory that shall be reallocated
   4.129 + * @param n the new size of the memory
   4.130 + * @return a pointer to the new location of the memory
   4.131 + * @see ucx_allocator_realloc()
   4.132 + */
   4.133 +void *ucx_stack_realloc(UcxStack *stack, void *ptr, size_t n);
   4.134 +
   4.135 +/**
   4.136 + * Frees memory on the stack.
   4.137 + * 
   4.138 + * Freeing stack memory behaves in a special way.
   4.139 + * 
   4.140 + * If the element, that should be freed, is the top most element of the stack,
   4.141 + * it is removed from the stack. Otherwise it is marked as freed. Marked
   4.142 + * elements are removed, when they become the top most elements of the stack.
   4.143 + * 
   4.144 + * @param stack a pointer to the stack
   4.145 + * @param ptr a pointer to the memory that shall be freed
   4.146 + */
   4.147 +void ucx_stack_free(UcxStack *stack, void *ptr);
   4.148 +
   4.149 +
   4.150 +/**
   4.151 + * Returns the size of the top most element.
   4.152 + * @param stack a pointer to the stack
   4.153 + * @return the size of the top most element
   4.154 + */
   4.155 +#define ucx_stack_topsize(stack) (*(((size_t*)stack->top - 1))
   4.156 +
   4.157 +/**
   4.158 + * Removes the top most element from the stack and copies the content to <code>
   4.159 + * dest</code>, if specified.
   4.160 + * 
   4.161 + * Use #ucx_stack_topsize()# to get the amount of memory that must be available
   4.162 + * at the location of <code>dest</code>.
   4.163 + * 
   4.164 + * @param stack a pointer to the stack
   4.165 + * @param dest the location where the contents shall be written to, or <code>
   4.166 + * NULL</code>, if the element shall only be removed.
   4.167 + * @see ucx_stack_free
   4.168 + * @see ucx_stack_popn
   4.169 + */
   4.170 +void ucx_stack_pop(UcxStack *stack, void *dest);
   4.171 +
   4.172 +/**
   4.173 + * Removes the top most element from the stack and copies the content to <code>
   4.174 + * dest</code>.
   4.175 + * 
   4.176 + * In contrast to #ucx_stack_pop() the <code>dest</code> pointer <code>MUST
   4.177 + * NOT</code> be <code>NULL</code>.
   4.178 + * 
   4.179 + * @param stack a pointer to the stack
   4.180 + * @param dest the location where the contents shall be written to
   4.181 + * @param n copies at most n elements to <code>dest</code>
   4.182 + * @see ucx_stack_pop
   4.183 + */
   4.184 +void ucx_stack_popn(UcxStack *stack, void *dest, size_t n);
   4.185 +
   4.186 +/**
   4.187 + * Returns the remaining available memory on the specified stack.
   4.188 + * 
   4.189 + * @param stack a pointer to the stack
   4.190 + * @return the remaining available memory
   4.191 + */
   4.192 +#define ucx_stack_avail(stack) ((stack->size)  - (s.top - s.space)\
   4.193 +                                - sizeof(size_t))
   4.194 +
   4.195 +
   4.196 +#ifdef	__cplusplus
   4.197 +}
   4.198 +#endif
   4.199 +
   4.200 +#endif	/* UCX_STACK_H */
   4.201 +
     5.1 --- a/ucx/string.c	Mon Jul 14 13:51:02 2014 +0200
     5.2 +++ b/ucx/string.c	Mon Jul 14 16:54:10 2014 +0200
     5.3 @@ -90,7 +90,7 @@
     5.4      }
     5.5      
     5.6      // create new string
     5.7 -    str.ptr = malloc(strlen + 1);
     5.8 +    str.ptr = almalloc(a, strlen + 1);
     5.9      str.length = strlen;
    5.10      if(!str.ptr) {
    5.11          free(strings);
     6.1 --- a/ucx/utils.c	Mon Jul 14 13:51:02 2014 +0200
     6.2 +++ b/ucx/utils.c	Mon Jul 14 16:54:10 2014 +0200
     6.3 @@ -54,18 +54,22 @@
     6.4          return 0;
     6.5      }
     6.6      
     6.7 +    char *lbuf;    
     6.8      size_t ncp = 0;
     6.9 -    if (!buf) {
    6.10 -        buf = (char*)malloc(bufsize);
    6.11 -        if(buf == NULL) {
    6.12 +    
    6.13 +    if(buf) {
    6.14 +        lbuf = buf;
    6.15 +    } else {
    6.16 +        lbuf = (char*)malloc(bufsize);
    6.17 +        if(lbuf == NULL) {
    6.18              return 0;
    6.19          }
    6.20      }
    6.21      
    6.22      size_t r;
    6.23      size_t rn = bufsize > n ? n : bufsize;
    6.24 -    while((r = readfnc(buf, 1, rn, src)) != 0) {
    6.25 -        r = writefnc(buf, 1, r, dest);
    6.26 +    while((r = readfnc(lbuf, 1, rn, src)) != 0) {
    6.27 +        r = writefnc(lbuf, 1, r, dest);
    6.28          ncp += r;
    6.29          n -= r;
    6.30          rn = bufsize > n ? n : bufsize;
    6.31 @@ -74,7 +78,10 @@
    6.32          }
    6.33      }
    6.34      
    6.35 -    free(buf);
    6.36 +    if (lbuf != buf) {
    6.37 +        free(lbuf);
    6.38 +    }
    6.39 +    
    6.40      return ncp;
    6.41  }
    6.42  

mercurial