Back to index

php5  5.3.10
fpm_arrays.h
Go to the documentation of this file.
00001 
00002        /* $Id: fpm_arrays.h,v 1.2 2008/05/24 17:38:47 anight Exp $ */
00003        /* (c) 2007,2008 Andrei Nigmatulin */
00004 
00005 #ifndef FPM_ARRAYS_H
00006 #define FPM_ARRAYS_H 1
00007 
00008 #include <stdlib.h>
00009 #include <string.h>
00010 
00011 struct fpm_array_s {
00012        void *data;
00013        size_t sz;
00014        size_t used;
00015        size_t allocated;
00016 };
00017 
00018 static inline struct fpm_array_s *fpm_array_init(struct fpm_array_s *a, unsigned int sz, unsigned int initial_num) /* {{{ */
00019 {
00020        void *allocated = 0;
00021 
00022        if (!a) {
00023               a = malloc(sizeof(struct fpm_array_s));
00024 
00025               if (!a) {
00026                      return 0;
00027               }
00028 
00029               allocated = a;
00030        }
00031 
00032        a->sz = sz;
00033 
00034        a->data = calloc(sz, initial_num);
00035 
00036        if (!a->data) {
00037               free(allocated);
00038               return 0;
00039        }
00040 
00041        a->allocated = initial_num;
00042        a->used = 0;
00043 
00044        return a;
00045 }
00046 /* }}} */
00047 
00048 static inline void *fpm_array_item(struct fpm_array_s *a, unsigned int n) /* {{{ */
00049 {
00050        char *ret;
00051 
00052        ret = (char *) a->data + a->sz * n;
00053 
00054        return ret;
00055 }
00056 /* }}} */
00057 
00058 static inline void *fpm_array_item_last(struct fpm_array_s *a) /* {{{ */
00059 {
00060        return fpm_array_item(a, a->used - 1);
00061 }
00062 /* }}} */
00063 
00064 static inline int fpm_array_item_remove(struct fpm_array_s *a, unsigned int n) /* {{{ */
00065 {
00066        int ret = -1;
00067 
00068        if (n < a->used - 1) {
00069               void *last = fpm_array_item(a, a->used - 1);
00070               void *to_remove = fpm_array_item(a, n);
00071 
00072               memcpy(to_remove, last, a->sz);
00073 
00074               ret = n;
00075        }
00076 
00077        --a->used;
00078 
00079        return ret;
00080 }
00081 /* }}} */
00082 
00083 static inline void *fpm_array_push(struct fpm_array_s *a) /* {{{ */
00084 {
00085        void *ret;
00086 
00087        if (a->used == a->allocated) {
00088               size_t new_allocated = a->allocated ? a->allocated * 2 : 20;
00089               void *new_ptr = realloc(a->data, a->sz * new_allocated);
00090 
00091               if (!new_ptr) {
00092                      return 0;
00093               }
00094 
00095               a->data = new_ptr;
00096               a->allocated = new_allocated;
00097        }
00098 
00099        ret = fpm_array_item(a, a->used);
00100 
00101        ++a->used;
00102 
00103        return ret;
00104 }
00105 /* }}} */
00106 
00107 static inline void fpm_array_free(struct fpm_array_s *a) /* {{{ */
00108 {
00109        free(a->data);
00110        a->data = 0;
00111        a->sz = 0;
00112        a->used = a->allocated = 0;
00113 }
00114 /* }}} */
00115 
00116 #endif