root/sapi/fpm/fpm/fpm_arrays.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. fpm_array_init
  2. fpm_array_item
  3. fpm_array_item_last
  4. fpm_array_item_remove
  5. fpm_array_push
  6. fpm_array_free

   1 
   2         /* $Id: fpm_arrays.h,v 1.2 2008/05/24 17:38:47 anight Exp $ */
   3         /* (c) 2007,2008 Andrei Nigmatulin */
   4 
   5 #ifndef FPM_ARRAYS_H
   6 #define FPM_ARRAYS_H 1
   7 
   8 #include <stdlib.h>
   9 #include <string.h>
  10 
  11 struct fpm_array_s {
  12         void *data;
  13         size_t sz;
  14         size_t used;
  15         size_t allocated;
  16 };
  17 
  18 static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num) /* {{{ */
  19 {
  20         void *allocated = 0;
  21 
  22         if (!a) {
  23                 a = malloc(sizeof(struct fpm_array_s));
  24 
  25                 if (!a) {
  26                         return 0;
  27                 }
  28 
  29                 allocated = a;
  30         }
  31 
  32         a->sz = sz;
  33 
  34         a->data = calloc(sz, initial_num);
  35 
  36         if (!a->data) {
  37                 free(allocated);
  38                 return 0;
  39         }
  40 
  41         a->allocated = initial_num;
  42         a->used = 0;
  43 
  44         return a;
  45 }
  46 /* }}} */
  47 
  48 static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n) /* {{{ */
  49 {
  50         char *ret;
  51 
  52         ret = (char *) a->data + a->sz * n;
  53 
  54         return ret;
  55 }
  56 /* }}} */
  57 
  58 static inline void *fpm_array_item_last(struct fpm_array_s *a) /* {{{ */
  59 {
  60         return fpm_array_item(a, a->used - 1);
  61 }
  62 /* }}} */
  63 
  64 static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n) /* {{{ */
  65 {
  66         int ret = -1;
  67 
  68         if (n < a->used - 1) {
  69                 void *last = fpm_array_item(a, a->used - 1);
  70                 void *to_remove = fpm_array_item(a, n);
  71 
  72                 memcpy(to_remove, last, a->sz);
  73 
  74                 ret = n;
  75         }
  76 
  77         --a->used;
  78 
  79         return ret;
  80 }
  81 /* }}} */
  82 
  83 static inline void *fpm_array_push(struct fpm_array_s *a) /* {{{ */
  84 {
  85         void *ret;
  86 
  87         if (a->used == a->allocated) {
  88                 size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
  89                 void *new_ptr = realloc(a->data, a->sz * new_allocated);
  90 
  91                 if (!new_ptr) {
  92                         return 0;
  93                 }
  94 
  95                 a->data = new_ptr;
  96                 a->allocated = new_allocated;
  97         }
  98 
  99         ret = fpm_array_item(a, a->used);
 100 
 101         ++a->used;
 102 
 103         return ret;
 104 }
 105 /* }}} */
 106 
 107 static inline void fpm_array_free(struct fpm_array_s *a) /* {{{ */
 108 {
 109         free(a->data);
 110         a->data = 0;
 111         a->sz = 0;
 112         a->used = a->allocated = 0;
 113 }
 114 /* }}} */
 115 
 116 #endif

/* [<][>][^][v][top][bottom][index][help] */