bitmap.c revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2015 Tom Gundersen
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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 systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "alloc-util.h"
#include "bitmap.h"
#include "hashmap.h"
#include "macro.h"
struct Bitmap {
};
/* Bitmaps are only meant to store relatively small numbers
* (corresponding to, say, an enum), so it is ok to limit
* the max entry. 64k should be plenty. */
#define BITMAPS_MAX_ENTRY 0xffff
/* This indicates that we reached the end of the bitmap */
#define BITMAP_END ((unsigned) -1)
Bitmap *bitmap_new(void) {
}
void bitmap_free(Bitmap *b) {
if (!b)
return;
free(b);
}
int bitmap_ensure_allocated(Bitmap **b) {
Bitmap *a;
assert(b);
if (*b)
return 0;
a = bitmap_new();
if (!a)
return -ENOMEM;
*b = a;
return 0;
}
int bitmap_set(Bitmap *b, unsigned n) {
unsigned offset;
assert(b);
/* we refuse to allocate huge bitmaps */
if (n > BITMAPS_MAX_ENTRY)
return -ERANGE;
offset = BITMAP_NUM_TO_OFFSET(n);
return -ENOMEM;
}
return 0;
}
void bitmap_unset(Bitmap *b, unsigned n) {
unsigned offset;
if (!b)
return;
offset = BITMAP_NUM_TO_OFFSET(n);
return;
}
bool bitmap_isset(Bitmap *b, unsigned n) {
unsigned offset;
if (!b)
return false;
offset = BITMAP_NUM_TO_OFFSET(n);
return false;
}
bool bitmap_isclear(Bitmap *b) {
unsigned i;
assert(b);
for (i = 0; i < b->n_bitmaps; i++)
if (b->bitmaps[i] != 0)
return false;
return true;
}
void bitmap_clear(Bitmap *b) {
assert(b);
b->n_bitmaps = 0;
b->bitmaps_allocated = 0;
}
assert(i);
assert(n);
if (!b || i->idx == BITMAP_END)
return false;
i->idx = *n + 1;
return true;
}
}
}
rem = 0;
bitmask = 1;
}
i->idx = BITMAP_END;
return false;
}
Bitmap *c;
unsigned i;
if (!a ^ !b)
return false;
if (!a)
return true;
return false;
for (i = common_n_bitmaps; i < c->n_bitmaps; i++)
if (c->bitmaps[i] != 0)
return false;
return true;
}