asstorage.h

Go to the documentation of this file.
00001 #ifndef ASSTORAGE_H_HEADER_INCLUDED
00002 #define ASSTORAGE_H_HEADER_INCLUDED
00003 
00004 
00005 #define AS_STORAGE_PAGE_SIZE            4096
00006 
00007 /* 
00008  *      there could be up to 16 arrays of 1024 pointers to slots each in Storage Block
00009  *      There could be 2^18 StorageBlocks in ASStorage
00010  */
00011 #define AS_STORAGE_SLOTS_BATCH          1024  /* we allocate pointers to slots in batches of one page eache  */ 
00012 #define AS_STORAGE_SLOT_ID_BITS         14  /* 32*512 == 2^14 */ 
00013 #define AS_STORAGE_MAX_SLOTS_CNT        (0x01<<AS_STORAGE_SLOT_ID_BITS)
00014 
00015 #define AS_STORAGE_BLOCK_ID_BITS        (32-AS_STORAGE_SLOT_ID_BITS)
00016 #define AS_STORAGE_MAX_BLOCK_CNT        (0x01<<AS_STORAGE_BLOCK_ID_BITS)
00017 /* #define AS_STORAGE_DEF_BLOCK_SIZE    (1024*256)  */
00018 #define AS_STORAGE_DEF_BLOCK_SIZE       (1024*128)  /* 128 Kb */  
00019 #define AS_STORAGE_NOUSE_THRESHOLD      (1024*8)  /* 8 Kb if total_free < 8K we should not try and use that 
00020                                                                                            *  block as we may fall into trap constantly defragmenting it
00021                                                                                            *  so we prefer to leave memory unused since 2 pages is not too much to loose */  
00022 
00023 
00024 #define ASStorageSlot_SIZE 16 /* 16 bytes */
00025 #define ASStorageSlot_USABLE_SIZE(slot) (((slot)->size+15)&0x8FFFFFF0)
00026 #define ASStorageSlot_FULL_SIZE(slot) (ASStorageSlot_USABLE_SIZE(slot)+ASStorageSlot_SIZE)
00027 /* space for slots is allocated in 16 byte increments */
00028 #define AS_STORAGE_GetNextSlot(slot) ((slot)+1+(ASStorageSlot_USABLE_SIZE(slot)>>4))
00029 
00030 
00031 /* RLE encoding of difference 
00032  * We calculate difference between following bytes. If differece is zero - its RLE encoded.
00033  * If its +-1 - its encoded as 2 bit values
00034  * If Its +-(from 2 to 7) - its encoded using 4 bit values
00035  * If Its +-(from 8 to 127) - its encoded using 8 bit values  
00036  * If Its +-(from 128 to 255) - its encoded using 9 bit values
00037  * 
00038  * The hope is that most of the bytes will be reduced to 0 
00039  * The next likely value will be from 2 to 7 
00040  * and only few cases will fall in other categories
00041  * 
00042  * For bitmaps we store lengths of ones and zerous, assuming that each string tsarts with 0
00043  * 
00044  * */
00045 
00046 /* The following lines is used only for non-bitmaps : */
00047 #define RLE_ZERO_MASK                           0x0080  /* M        */  
00048 #define RLE_ZERO_LENGTH                         0x007F  /*  LLLLLLL */  
00049 #define RLE_ZERO_SIG                            0x0000  /* 0LLLLLLL - identical to a string of LLLLLLL zeros */  
00050 
00051 #define RLE_NOZERO_SHORT_MASK           0x00C0  /* MM       */  
00052 #define RLE_NOZERO_SHORT_LENGTH         0x003F  /*   LLLLLL */  
00053 #define RLE_NOZERO_SHORT_SIG            0x00C0  /* 11LLLLLL followed by stream of LLLLLL 4 or 2 bit values */
00054 
00055 #define RLE_NOZERO_LONG_MASK            0x00F0  /* MMMM     */  
00056 #define RLE_NOZERO_LONG_LENGTH          0x000F  /*     LLLL */  
00057 #define RLE_NOZERO_LONG1_SIG            0x00A0  /* 1010LLLL followed by stream of LLLL 2 or 4 bit values */  
00058 #define RLE_NOZERO_LONG2_SIG            0x00B0  /* 1011LLLL followed by stream of LLLL 1 byte values */  
00059 
00060 #define RLE_9BIT_SIG                            0x0080  /* 1000LLLL followed by stream of LLLL 1 byte values 
00061                                                that change sign from byte to byte starting with positive */     
00062 #define RLE_9BIT_NEG_SIG                        0x0090  /* 1001LLLL followed by stream of LLLL 1 byte values 
00063                                                that change sign from byte to byte starting with negative */     
00064 
00065 #define AS_STORAGE_DEFAULT_BMAP_THRESHOLD 0x7F
00066 #define AS_STORAGE_DEFAULT_BMAP_VALUE     0xFF
00067 
00068 
00069 typedef struct ASStorageSlot
00070 {
00071 /* Pointer to ASStorageSlot is the pointer to used memory beginning - ASStorageSlot_SIZE 
00072  * thus we need not to store it separately 
00073  */
00074 #define ASStorage_ZlibCompress          (0x01<<0)  /* do we really want that ? */ 
00075 #define ASStorage_RLEDiffCompress       (0x01<<1)  /* RLE of difference */ 
00076 
00077 #define ASStorage_CompressionType       (0x0F<<0)  /* allow for 16 compression schemes */
00078 #define ASStorage_Used                          (0x01<<4)
00079 #define ASStorage_NotTileable           (0x01<<5)
00080 #define ASStorage_Reference                     (0x01<<6)  /* data is the id of some other slot */ 
00081 #define ASStorage_Bitmap                        (0x01<<7)  /* data is 1 bpp */ 
00082 #define ASStorage_32Bit                         (0x01<<8)  /* data is 32 bpp with only first 8 bits being significant */ 
00083 #define ASStorage_BitShiftFlagPos   9
00084 #define ASStorage_BitShift                      (0x03<<ASStorage_BitShiftFlagPos)  
00085 #define ASStorage_8BitShift                     (0x01<<ASStorage_BitShiftFlagPos)  
00086                                                                                                 /* data is 32 bpp shifted left by 8 bit 
00087                                                                                                  * (must combine with _32Bit flag )*/ 
00088 #define ASStorage_16BitShift            (0x01<<(ASStorage_BitShiftFlagPos+1)) 
00089                                                                                                 /* data is 32 bpp shifted left by 16 bit 
00090                                                                                                 * (must combine with _32Bit flag )
00091                                                                                                 * If combined with 8BitShift - results in 24 bit shift */ 
00092 #define ASStorage_24BitShift            (ASStorage_8BitShift|ASStorage_16BitShift)
00093 #define ASStorage_Flags2ShiftIdx(f) (((f)>>ASStorage_BitShiftFlagPos)&0x03)
00094 #define ASStorage_Flags2Shift(f)        (ASStorage_Flags2ShiftIdx(f)*8)
00095 #define ASStorage_Masked                        (0x01<<11) /* mask 32bit value to filter out higher 24 bits
00096                                                 * if combined with BitShift - bitshift is done 
00097                                                                                                 * prior to masking */ 
00098 
00099 
00100 #define ASStorage_32BitRLE                      (ASStorage_RLEDiffCompress|ASStorage_32Bit)
00101 
00102         CARD16  flags ;
00103         CARD16  ref_count ;
00104         CARD32  size ;
00105         CARD32  uncompressed_size ;
00106         CARD16  index ;  /* reverse mapping of slot address into index in array */
00107         /* slots may be placed in array pointing into different areas of the memory 
00108          * block, since we will need to implement some sort of garbadge collection and 
00109          * defragmentation mechanism - we need to be able to process them in orderly    
00110          * fashion. 
00111          * So finally : 
00112          * 1) slot's index does not specify where in the memory slot 
00113          * is located, it is only used to address slot from outside.
00114          * 2) Using slots memory address and its size we can go through the chain of slots
00115          * and perform all the maintenance tasks  as long as we have reverse mapping 
00116          * of addresses into indexes.
00117          * 
00118          */
00119         CARD16 reserved ;          /* to make us have size rounded by 16 bytes margin */
00120         /* Data immidiately follows here : 
00121          * CARD8   data[0] ; */
00122 
00123 #define ASStorage_Data(s)  ((CARD8*)((s)+1))
00124 
00125 }ASStorageSlot;
00126 
00127 /* turns out there is no performance gains from using int here instead of short - 
00128 so save some memory if we can : */
00129 typedef short ASStorageDiff;
00130 
00131 
00132 typedef void (*compute_diff_func_type)(ASStorageDiff*,CARD8*,int);
00133 typedef int  (*copy_data32_func_type)(CARD8*,CARD32*,int);
00134 typedef int  (*copy_data32_tinted_func_type)(CARD8*,CARD32*,int,CARD32);
00135 
00136 
00137 typedef struct ASStorageBlock
00138 {
00139 #define ASStorage_MonoliticBlock                (0x01<<0) /* block consists of a single batch of storage */
00140         CARD32  flags ;
00141         int     size ;
00142 
00143         int     total_free;
00144         ASStorageSlot  *start, *end;
00145         /* array of pointers to slots is allocated separately, so that we can reallocate it 
00146            in case we have lots of small slots */
00147         ASStorageSlot **slots;
00148         int slots_count, unused_count ;
00149         int first_free, last_used ;
00150         int long_searches ;
00151 
00152 }ASStorageBlock;
00153 
00154 typedef struct ASStorage
00155 {
00156         int default_block_size ;
00157 
00158 
00159         ASStorageBlock **blocks ;
00160         int                     blocks_count;
00161 
00162         ASStorageDiff  *diff_buf ;
00163         CARD8  *comp_buf ;
00164         size_t  comp_buf_size ; 
00165 
00166 }ASStorage;
00167 
00168 
00169 typedef CARD32 ASStorageID ;
00170 
00171 ASStorageID store_data(ASStorage *storage, CARD8 *data, int size, ASFlagType flags, CARD8 bitmap_threshold);
00172 ASStorageID store_data_tinted(ASStorage *storage, CARD8 *data, int size, ASFlagType flags, CARD16 tint);
00173 
00174 /* data will be fetched fromthe slot identified by id and placed into buffer. 
00175  * Data will be fetched from offset  and will count buf_size bytes if buf_size is greater then
00176  * available data - data will be tiled to accomodate this size, unless NotTileable is set */
00177 int  fetch_data(ASStorage *storage, ASStorageID id, CARD8 *buffer, int offset, int buf_size, CARD8 bitmap_value, int *original_size);
00178 int  fetch_data32(ASStorage *storage, ASStorageID id, CARD32 *buffer, int offset, int buf_size, CARD8 bitmap_value, int *original_size);
00179 int  threshold_stored_data(ASStorage *storage, ASStorageID id, unsigned int *runs, int width, unsigned int threshold);
00180 
00181 /* slot identified by id will be marked as unused */
00182 void forget_data(ASStorage *storage, ASStorageID id);
00183 
00184 void print_storage(ASStorage *storage);
00185 
00186 int print_storage_slot(ASStorage *storage, ASStorageID id);
00187 Bool query_storage_slot(ASStorage *storage, ASStorageID id, ASStorageSlot *dst );
00188 
00189 /* returns new ID without copying data. Data will be stored as copy-on-right. 
00190  * Reference count of the data will be increased. If optional dst_id is specified - 
00191  * its data will be erased, and it will point to the data of src_id: 
00192  */                             
00193 ASStorageID dup_data(ASStorage *storage, ASStorageID src_id);
00194 
00195 /* this will provide access to default storage heap that is used whenever above functions get
00196  * NULL passed as ASStorage parameter :
00197  */
00198 void flush_default_asstorage();
00199 int set_asstorage_block_size( ASStorage *storage, int new_size );
00200 
00201 
00202 #endif

Generated on Tue Jul 5 14:13:26 2011 for ROOT_528-00b_version by  doxygen 1.5.1