bitmap.c 4.4 KB
Newer Older
1 2 3
/*
 * bitmap.h: Simple bitmap operations
 *
4
 * Copyright (C) 2010-2011 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2010 Novell, Inc.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
O
Osier Yang 已提交
18 19
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * Author: Jim Fehlig <jfehlig@novell.com>
 */

#include <config.h>

#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>

#include "bitmap.h"
#include "memory.h"
35
#include "buf.h"
36 37 38


struct _virBitmap {
39 40
    size_t max_bit;
    size_t map_len;
41
    unsigned long *map;
42 43 44
};


45
#define VIR_BITMAP_BITS_PER_UNIT  ((int) sizeof(unsigned long) * CHAR_BIT)
46 47
#define VIR_BITMAP_UNIT_OFFSET(b) ((b) / VIR_BITMAP_BITS_PER_UNIT)
#define VIR_BITMAP_BIT_OFFSET(b)  ((b) % VIR_BITMAP_BITS_PER_UNIT)
48
#define VIR_BITMAP_BIT(b)         (1UL << VIR_BITMAP_BIT_OFFSET(b))
49 50 51


/**
52
 * virBitmapNew:
53 54 55 56 57 58 59
 * @size: number of bits
 *
 * Allocate a bitmap capable of containing @size bits.
 *
 * Returns a pointer to the allocated bitmap or NULL if
 * memory cannot be allocated.
 */
60
virBitmapPtr virBitmapNew(size_t size)
61 62 63 64
{
    virBitmapPtr bitmap;
    size_t sz;

E
Eric Blake 已提交
65
    if (SIZE_MAX - VIR_BITMAP_BITS_PER_UNIT < size || size == 0)
66 67 68 69 70 71 72 73 74 75 76 77 78
        return NULL;

    sz = (size + VIR_BITMAP_BITS_PER_UNIT - 1) /
          VIR_BITMAP_BITS_PER_UNIT;

    if (VIR_ALLOC(bitmap) < 0)
        return NULL;

    if (VIR_ALLOC_N(bitmap->map, sz) < 0) {
        VIR_FREE(bitmap);
        return NULL;
    }

79 80
    bitmap->max_bit = size;
    bitmap->map_len = sz;
81 82 83 84 85 86 87
    return bitmap;
}

/**
 * virBitmapFree:
 * @bitmap: previously allocated bitmap
 *
88
 * Free @bitmap previously allocated by virBitmapNew.
89 90 91 92 93 94 95 96 97
 */
void virBitmapFree(virBitmapPtr bitmap)
{
    if (bitmap) {
        VIR_FREE(bitmap->map);
        VIR_FREE(bitmap);
    }
}

D
Daniel P. Berrange 已提交
98 99 100

int virBitmapCopy(virBitmapPtr dst, virBitmapPtr src)
{
101
    if (dst->max_bit != src->max_bit) {
D
Daniel P. Berrange 已提交
102 103 104 105
        errno = EINVAL;
        return -1;
    }

106
    memcpy(dst->map, src->map, src->map_len * sizeof(src->map[0]));
D
Daniel P. Berrange 已提交
107 108 109 110 111

    return 0;
}


112 113 114 115 116 117 118 119 120 121 122
/**
 * virBitmapSetBit:
 * @bitmap: Pointer to bitmap
 * @b: bit position to set
 *
 * Set bit position @b in @bitmap
 *
 * Returns 0 on if bit is successfully set, -1 on error.
 */
int virBitmapSetBit(virBitmapPtr bitmap, size_t b)
{
123
    if (bitmap->max_bit <= b)
124 125
        return -1;

126
    bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] |= VIR_BITMAP_BIT(b);
127 128 129 130 131 132 133 134 135 136 137 138 139 140
    return 0;
}

/**
 * virBitmapClearBit:
 * @bitmap: Pointer to bitmap
 * @b: bit position to clear
 *
 * Clear bit position @b in @bitmap
 *
 * Returns 0 on if bit is successfully clear, -1 on error.
 */
int virBitmapClearBit(virBitmapPtr bitmap, size_t b)
{
141
    if (bitmap->max_bit <= b)
142 143
        return -1;

144
    bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] &= ~VIR_BITMAP_BIT(b);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    return 0;
}

/**
 * virBitmapGetBit:
 * @bitmap: Pointer to bitmap
 * @b: bit position to get
 * @result: bool pointer to receive bit setting
 *
 * Get setting of bit position @b in @bitmap and store in @result
 *
 * On success, @result will contain the setting of @b and 0 is
 * returned.  On failure, -1 is returned and @result is unchanged.
 */
int virBitmapGetBit(virBitmapPtr bitmap, size_t b, bool *result)
{
161
    if (bitmap->max_bit <= b)
162 163
        return -1;

164
    *result = !!(bitmap->map[VIR_BITMAP_UNIT_OFFSET(b)] & VIR_BITMAP_BIT(b));
165 166
    return 0;
}
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

/**
 * virBitmapString:
 * @bitmap: Pointer to bitmap
 *
 * Convert @bitmap to printable string.
 *
 * Returns pointer to the string or NULL on error.
 */
char *virBitmapString(virBitmapPtr bitmap)
{
    virBuffer buf = VIR_BUFFER_INITIALIZER;
    size_t sz;

    virBufferAddLit(&buf, "0x");

183
    sz = bitmap->map_len;
184 185

    while (sz--) {
186
        virBufferAsprintf(&buf, "%0*lx",
187 188 189 190 191 192 193 194 195 196 197
                          VIR_BITMAP_BITS_PER_UNIT / 4,
                          bitmap->map[sz]);
    }

    if (virBufferError(&buf)) {
        virBufferFreeAndReset(&buf);
        return NULL;
    }

    return virBufferContentAndReset(&buf);
}