00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef STD_MEM_HAS_BEEN_INCLUDED
00010 #define STD_MEM_HAS_BEEN_INCLUDED
00011
00012
00013
00014
00015
00016 typedef char UGAgeneric;
00017 typedef UGAgeneric *UGAptr;
00018 typedef enum {
00019 STD_FALSE = 0,
00020 STD_TRUE = 1
00021 } STDbool;
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 class mem_push {
00039 public:
00040 mem_push(size_t datasize = sizeof(UGAgeneric));
00041 virtual ~mem_push();
00042
00043 STDbool is_empty () const { return num_objects == 0 ? STD_TRUE : STD_FALSE; }
00044
00045 void remove_all() {remove_top(0, count());}
00046
00047 protected:
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 void insert_top (UGAptr, size_t count = 1);
00058 void insert_bottom (const char *, size_t count = 1);
00059 size_t remove_top (UGAptr = NULL, size_t count = 1);
00060 size_t peek_top (UGAptr = NULL, size_t count = 1) const;
00061
00062 size_t count () const { return num_objects; }
00063
00064 private:
00065 void increase_mem (size_t);
00066 void decrease_mem (size_t);
00067
00068 size_t block_index (size_t offset) const { return offset / block_size; }
00069 size_t block_offset (size_t offset) const { return offset % block_size; }
00070 size_t block_left (size_t offset) const { return block_size - block_offset (offset); }
00071 UGAptr block_addr (size_t offset) const { return blocks[block_index(offset)] +
00072 block_offset (offset); }
00073
00074 UGAptr *blocks;
00075 size_t num_blocks;
00076 size_t num_objects;
00077 size_t obj_size, block_size;
00078 size_t top, bottom;
00079 };
00080
00081
00082
00083
00084
00085 class STDmem_queue : public mem_push {
00086 public:
00087 STDmem_queue (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00088
00089 void put (const char *data, size_t count = 1) { insert_bottom(data, count); }
00090 size_t get (UGAptr data=NULL, size_t count = 1) { return remove_top(data,count);}
00091 size_t peek (UGAptr data=NULL, size_t count = 1) const { return peek_top(data,count); }
00092 size_t count() const { return mem_push::count(); }
00093 };
00094
00095
00096
00097
00098
00099 class STDmem_stack : public mem_push {
00100 public:
00101 STDmem_stack (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00102
00103 void push (UGAptr data, size_t count = 1) { insert_top(data,count); }
00104 size_t pop (UGAptr data = NULL, size_t count = 1) { return remove_top(data,count); }
00105 size_t peek (UGAptr data = NULL, size_t count = 1) const { return peek_top(data,count); }
00106 size_t count() const { return mem_push::count(); }
00107 };
00108
00109
00110
00111
00112
00113 class STDmem_dequeue : public mem_push {
00114 public:
00115 STDmem_dequeue (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {}
00116
00117 void push (UGAptr data, size_t count = 1) { insert_top(data,count); }
00118 void put (UGAptr data, size_t count = 1) { insert_bottom(data,count); }
00119 size_t pop (UGAptr data = NULL, size_t count = 1) { return remove_top(data,count); }
00120 size_t peek (UGAptr data = NULL, size_t count = 1) const { return peek_top(data,count); }
00121 size_t count() const { return mem_push::count(); }
00122 };
00123
00124 #endif