00001
00002
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 #include "BitmapDigest.h"
00034 #include "Log.h"
00035 #include "LogIdSet.h"
00036 #include "mace-macros.h"
00037
00038
00039
00040
00041 bitmap_digest::bitmap_digest(int size)
00042 {
00043 ADD_FUNC_SELECTORS;
00044 if (size % 8 != 0)
00045 {
00046 maceError("size is not on multiple of 8, exiting: %d", size);
00047 exit(50);
00048 }
00049 #if VARIABLE_BITMAP
00050 bm.tab = new unsigned char [size/8];
00051 #endif
00052 bm.tab_size = size;
00053 bm.start_bit = 0;
00054 bm.start_seq = 0;
00055 bm.last_seq = 0;
00056 reset();
00057 }
00058
00059 bitmap_digest::~bitmap_digest()
00060 {
00061 }
00062
00063 int bitmap_digest::which_byte(int seq)
00064 {
00065 return (seq % bm.tab_size)/8;
00066 }
00067
00068 int bitmap_digest::which_bit(int seq)
00069 {
00070 return seq % 8;
00071 }
00072
00073 int bitmap_digest::in_table(int seq)
00074 {
00075 if (seq < bm.start_seq ||seq > bm.last_seq)
00076 return 0;
00077 return 1;
00078 }
00079
00080 void bitmap_digest::set_bit(int seq, int set_value)
00081 {
00082 unsigned char value = 0x01;
00083 value = value<<which_bit(seq);
00084 int which= which_byte(seq);
00085
00086 if (set_value)
00087 {
00088 bm.tab[which] = (bm.tab[which]|value);
00089 }
00090 else
00091 {
00092 bm.tab[which] = (bm.tab[which]&(~value));
00093 }
00094 }
00095
00096 bool bitmap_digest::contains(int seq)
00097 {
00098 if (! in_table(seq)) {
00099 return 0;
00100 }
00101 unsigned char value = bm.tab[ which_byte(seq)];
00102 value = value>>which_bit(seq);
00103 return (value&0x01);
00104 }
00105
00106 int bitmap_digest::insert (int item, int suppress)
00107 {
00108 ADD_FUNC_SELECTORS;
00109
00110
00111 if (contains(item)) {
00112
00113 return 0;
00114 }
00115 if (item > bm.last_seq)
00116 bm.last_seq = item;
00117 if (bm.last_seq-bm.start_seq+1 > bm.tab_size) {
00118 int old_start = bm.start_seq;
00119 bm.start_seq = bm.last_seq-bm.tab_size+1;
00120 maceDebug(1,"bitmap_digest::insert bm.start_seq : %d", bm.start_seq );
00121
00122 for (int i = old_start; i < bm.start_seq-1 ;i++ )
00123 {
00124 set_bit(i, 0);
00125 }
00126 }
00127 set_bit(item, 1);
00128 return bm.start_seq;
00129 }
00130
00131 int bitmap_digest::remove_item (int item)
00132 {
00133 if (! in_table( item)) {
00134 return 0;
00135 }
00136 set_bit(item, 0);
00137 return 1;
00138 }
00139
00140 int bitmap_digest::get_lowest_sequence()
00141 {
00142 return bm.start_seq;
00143 }
00144
00145 int bitmap_digest::get_highest_sequence()
00146 {
00147 return bm.last_seq;
00148 }
00149
00150
00151
00152
00153
00154 int bitmap_digest::size_compacted_in_bytes ()
00155 {
00156 #if !VARIABLE_BITMAP
00157 return sizeof(bm);
00158 #else
00159 return size_core_in_bytes ()+bm.tab_size/8;
00160 #endif
00161 }
00162
00163
00164
00165
00166
00167
00168 void bitmap_digest::serialize (unsigned char* buffer)
00169 {
00170 #if !VARIABLE_BITMAP
00171 memmove(buffer, (unsigned char*)&bm,sizeof(bm));
00172 #else
00173 memmove(buffer, (unsigned char*)&bm, size_core_in_bytes ());
00174 memmove(buffer+size_core_in_bytes (), bm.tab, bm.tab_size/8);
00175 #endif
00176 }
00177
00178
00179
00180
00181
00182 void bitmap_digest::import (unsigned char* buffer)
00183 {
00184
00185 #if !VARIABLE_BITMAP
00186 memmove((unsigned char*)&bm, buffer, sizeof(bm));
00187 #else
00188 int existing_size = bm.tab_size;
00189 memmove((unsigned char*)&bm, buffer, size_core_in_bytes ());
00190 if (bm.tab && existing_size != bm.tab_size)
00191 {
00192 delete bm.tab;
00193 bm.tab = NULL;
00194 }
00195
00196 if (!bm.tab)
00197 {
00198 bm.tab = new unsigned char [bm.tab_size/8];
00199 }
00200
00201 memmove(bm.tab, buffer+size_core_in_bytes (), bm.tab_size/8);
00202 #endif
00203 }
00204
00205
00206
00207
00208
00209 void bitmap_digest::reset ()
00210 {
00211 for (int i=0; i<bm.tab_size/8; i++)
00212 {
00213 bm.tab[i] = 0;
00214 }
00215 }