00001 /* 00002 * Copyright 1997, Brown University, Providence, RI. 00003 * 00004 * All Rights Reserved 00005 * 00006 * Permission to use, copy, modify, and distribute this software and its 00007 * documentation for any purpose other than its incorporation into a 00008 * commercial product is hereby granted without fee, provided that the 00009 * above copyright notice appear in all copies and that both that 00010 * copyright notice and this permission notice appear in supporting 00011 * documentation, and that the name of Brown University not be used in 00012 * advertising or publicity pertaining to distribution of the software 00013 * without specific, written prior permission. 00014 * 00015 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 00016 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY 00017 * PARTICULAR PURPOSE. IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR 00018 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00019 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00020 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 00021 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00022 */ 00023 /* Copyright 1992, Brown Computer Graphics Group. All Rights Reserved. */ 00024 00025 /* ------------------------------------------------------------------------- 00026 * 00027 * < File description here > 00028 * 00029 * ------------------------------------------------------------------------- */ 00030 00031 #ifndef STD_MEM_HAS_BEEN_INCLUDED 00032 #define STD_MEM_HAS_BEEN_INCLUDED 00033 00034 /* -------------------------- Constants ----------------------------- */ 00035 00036 /* -------------------------- Types ----------------------------- */ 00037 00038 typedef char UGAgeneric; 00039 typedef UGAgeneric *UGAptr; 00040 typedef enum { 00041 STD_FALSE = 0, 00042 STD_TRUE = 1 00043 } STDbool; 00044 00045 /* -------------------------- Classes ----------------------------- */ 00046 00047 /* 00048 * TITLE : Stack/Queue Storage Class 00049 * 00050 * DESCR : This private class provides simple queue and/or stack 00051 * functionality for data objects. Means of data access are 00052 * not restricted to LIFO or FIFO; enforcement of stack/queue 00053 * semantics is left up to child objects. 00054 * 00055 * DETAILS : In order to make data i/o as efficient as possible with a 00056 * minimum of reallocations, the data is stored in a series of 00057 * rotating buffers, with byte offsets used to mark the 00058 * current push and pop positions. 00059 */ 00060 class mem_push { 00061 public: 00062 mem_push(size_t datasize = sizeof(UGAgeneric)); 00063 virtual ~mem_push(); 00064 00065 STDbool is_empty () const { return num_objects == 0 ? STD_TRUE : STD_FALSE; } 00066 00067 void remove_all() {remove_top(0, count());} 00068 00069 protected: 00070 /* 00071 * DESCR : Basic pushoperations, providing double-ended data 00072 * access. 00073 * 00074 * DETAILS : Note to class authors: in the current implementation, 00075 * the methods @insert_bottom@ and @remove_top@ are more 00076 * efficient than their counterparts and should be 00077 * preferred. 00078 */ 00079 void insert_top (UGAptr, size_t count = 1); 00080 void insert_bottom (const char *, size_t count = 1); 00081 size_t remove_top (UGAptr = NULL, size_t count = 1); 00082 size_t peek_top (UGAptr = NULL, size_t count = 1) const; 00083 00084 size_t count () const { return num_objects; } 00085 00086 private: 00087 void increase_mem (size_t); 00088 void decrease_mem (size_t); 00089 00090 size_t block_index (size_t offset) const { return offset / block_size; } 00091 size_t block_offset (size_t offset) const { return offset % block_size; } 00092 size_t block_left (size_t offset) const { return block_size - block_offset (offset); } 00093 UGAptr block_addr (size_t offset) const { return blocks[block_index(offset)] + 00094 block_offset (offset); } 00095 00096 UGAptr *blocks; 00097 size_t num_blocks; 00098 size_t num_objects; 00099 size_t obj_size, block_size; 00100 size_t top, bottom; 00101 }; 00102 00103 00104 /* 00105 * TITLE : Queue memory storage 00106 */ 00107 class STDmem_queue : public mem_push { 00108 public: 00109 STDmem_queue (size_t datasize = sizeof(UGAgeneric)):mem_push(datasize) {} 00110 00111 void put (const char *data, size_t count = 1) { insert_bottom(data, count); } 00112 size_t get (UGAptr data=NULL, size_t count = 1) { return remove_top(data,count);} 00113 size_t peek (UGAptr data=NULL, size_t count = 1) const { return peek_top(data,count); } 00114 size_t count() const { return mem_push::count(); } 00115 }; 00116 #endif