00001 #ifndef vnl_alloc_h_
00002 #define vnl_alloc_h_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <vcl_cstddef.h>
00036
00037 const int VNL_ALLOC_ALIGN = 8;
00038 const vcl_size_t VNL_ALLOC_MAX_BYTES = 256;
00039 const vcl_size_t VNL_ALLOC_NFREELISTS = VNL_ALLOC_MAX_BYTES/VNL_ALLOC_ALIGN;
00040
00041 class vnl_alloc {
00042
00043 private:
00044 static vcl_size_t ROUND_UP(vcl_size_t bytes) {
00045 return (((bytes) + VNL_ALLOC_ALIGN-1) & ~(VNL_ALLOC_ALIGN - 1));
00046 }
00047 private:
00048 union obj;
00049 friend union obj;
00050 union obj {
00051 union obj * free_list_link;
00052 char client_data[1];
00053 };
00054 private:
00055 # if defined ( __SUNPRO_CC ) || defined ( _AIX )
00056 static obj * free_list[];
00057
00058 # else
00059 static obj * free_list[VNL_ALLOC_NFREELISTS];
00060 # endif
00061 static vcl_size_t FREELIST_INDEX(vcl_size_t bytes) {
00062 return (((bytes) + VNL_ALLOC_ALIGN-1)/VNL_ALLOC_ALIGN - 1);
00063 }
00064
00065
00066 static void *refill(vcl_size_t n);
00067
00068
00069 static char *chunk_alloc(vcl_size_t size, int &nobjs);
00070
00071
00072 static char *start_free;
00073 static char *end_free;
00074 static vcl_size_t heap_size;
00075
00076 class lock {
00077 public:
00078 lock() { }
00079 ~lock() { }
00080 };
00081 friend class lock;
00082
00083 public:
00084
00085 typedef char value_type;
00086
00087
00088 static void * allocate(vcl_size_t n) {
00089 obj * * my_free_list;
00090 obj * result;
00091
00092 if (n > VNL_ALLOC_MAX_BYTES) {
00093 return (void*)new char[n];
00094 }
00095 my_free_list = free_list + FREELIST_INDEX(n);
00096
00097
00098
00099 result = *my_free_list;
00100 if (result == 0) {
00101 void *r = refill(ROUND_UP(n));
00102 return r;
00103 }
00104 *my_free_list = result -> free_list_link;
00105 return (result);
00106 };
00107
00108
00109 static void deallocate(void *p, vcl_size_t n)
00110 {
00111 obj *q = (obj *)p;
00112 obj * * my_free_list;
00113
00114 if (n > VNL_ALLOC_MAX_BYTES) {
00115 delete [] (char*)p;
00116 return;
00117 }
00118 my_free_list = free_list + FREELIST_INDEX(n);
00119 q -> free_list_link = *my_free_list;
00120 *my_free_list = q;
00121 }
00122
00123 static void * reallocate(void *p, vcl_size_t old_sz, vcl_size_t new_sz);
00124 };
00125
00126 # endif // vnl_alloc_h_