1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
//
// This file is part of GNU C++ uCommon.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include <ucommon/export.h>
#include <ucommon/bitmap.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
using namespace UCOMMON_NAMESPACE;
bitmap::bitmap(size_t count)
{
size_t mem = count / 8;
size = count;
bus = BMALLOC;
if(count % 8)
++mem;
addr.a = ::malloc(mem);
clear();
}
bitmap::bitmap(void *ptr, size_t count, bus_t access)
{
assert(ptr != NULL);
assert(access >= BMIN && access <= BMAX);
addr.a = ptr;
bus = access;
}
bitmap::~bitmap()
{
if(bus == BMALLOC && addr.b)
::free(addr.b);
addr.b = NULL;
}
unsigned bitmap::memsize(void) const
{
switch(bus) {
case B64:
return 64;
case B32:
return 32;
case B16:
return 16;
default:
return 8;
}
}
void bitmap::set(size_t offset, bool bit)
{
unsigned bs = memsize();
size_t pos = offset / bs;
unsigned rem = offset % bs;
uint64_t b64;
uint32_t b32;
uint16_t b16;
uint8_t b8;
if(offset >= size)
return;
switch(bus) {
case B64:
b64 = ((uint64_t)(1))<<rem;
if(bit)
addr.d[pos] |= b64;
else
addr.d[pos] &= ~b64;
break;
case B32:
b32 = 1<<rem;
if(bit)
addr.l[pos] |= b32;
else
addr.l[pos] &= ~b32;
break;
case B16:
b16 = 1<<rem;
if(bit)
addr.w[pos] |= b16;
else
addr.w[pos] &= ~b16;
break;
default:
b8 = 1<<rem;
if(bit)
addr.b[pos] |= b8;
else
addr.b[pos] &= ~b8;
break;
}
}
bool bitmap::get(size_t offset) const
{
unsigned bs = memsize();
size_t pos = offset / bs;
unsigned rem = offset % bs;
if(offset >= size)
return false;
switch(bus) {
#if !defined(_MSC_VER) || _MSC_VER >= 1400
case B64:
return (addr.d[pos] & 1ll<<rem) > 0;
#endif
case B32:
return (addr.l[pos] & 1l<<rem) > 0;
case B16:
return (addr.w[pos] & 1<<rem) > 0;
default:
return (addr.b[pos] & 1<<rem) > 0;
}
}
void bitmap::clear(void)
{
unsigned bs = memsize();
if(size % bs)
++size;
while(size--) {
switch(bus) {
#if !defined(_MSC_VER) || _MSC_VER >= 1400
case B64:
*(addr.d++) = 0ll;
break;
#endif
case B32:
*(addr.l++) = 0l;
break;
case B16:
*(addr.w++) = 0;
break;
default:
*(addr.b++) = 0;
}
}
}
|