Alexandre Lision | ddd731e | 2014-01-31 11:50:08 -0500 | [diff] [blame^] | 1 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 2 | // |
| 3 | // This file is part of GNU uCommon C++. |
| 4 | // |
| 5 | // GNU uCommon C++ is free software: you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU Lesser General Public License as published |
| 7 | // by the Free Software Foundation, either version 3 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // GNU uCommon C++ is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU Lesser General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU Lesser General Public License |
| 16 | // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | /** |
| 19 | * A simple class to perform bitmap manipulation. |
| 20 | * Bitmaps are used to manage bit-aligned objects, such as network cidr |
| 21 | * addresses. This header introduces a common bitmap management class |
| 22 | * for the ucommon library. |
| 23 | * @file ucommon/bitmap.h |
| 24 | */ |
| 25 | |
| 26 | #ifndef _UCOMMON_BITMAP_H_ |
| 27 | #define _UCOMMON_BITMAP_H_ |
| 28 | |
| 29 | #ifndef _UCOMMON_CONFIG_H_ |
| 30 | #include <ucommon/platform.h> |
| 31 | #endif |
| 32 | |
| 33 | NAMESPACE_UCOMMON |
| 34 | |
| 35 | /** |
| 36 | * A class to access bit fields in external bitmaps. The actual bitmap this |
| 37 | * object manipulates may not be stored in the object. Bitmaps may be |
| 38 | * referenced on special memory mapped or i/o bus based devices or other |
| 39 | * structures that have varying data word sizes which may differ from the |
| 40 | * default cpu bus size. The bitmap class can be set to the preferred memory |
| 41 | * bus size of the specific external bitmap being used. Bitmap size may also |
| 42 | * be relevant when accessing individual bits in memory mapped device registers |
| 43 | * where performing reference and manipulations may change the state of the |
| 44 | * device and hence must be aligned with the device register being effected. |
| 45 | * |
| 46 | * This class offers only the most basic bit manipulations, getting and |
| 47 | * setting individual bits in the bitmap. More advanced bit manipulations |
| 48 | * and other operations can be created in derived classes. |
| 49 | * @author David Sugar <dyfet@gnutelephony.org> |
| 50 | */ |
| 51 | class __EXPORT bitmap |
| 52 | { |
| 53 | protected: |
| 54 | size_t size; |
| 55 | |
| 56 | typedef union |
| 57 | { |
| 58 | void *a; |
| 59 | uint8_t *b; |
| 60 | uint16_t *w; |
| 61 | uint32_t *l; |
| 62 | uint64_t *d; |
| 63 | } addr_t; |
| 64 | |
| 65 | addr_t addr; |
| 66 | |
| 67 | public: |
| 68 | /** |
| 69 | * Specify data word size to use in accessing a bitmap. |
| 70 | */ |
| 71 | typedef enum { |
| 72 | BMALLOC, /**< Use default cpu size */ |
| 73 | B8, /**< Accessing a bitmap on 8 bit bus device */ |
| 74 | B16, /**< Accessing a bitmap on a 16 bit device */ |
| 75 | B32, /**< Accessing a bitmap on a 32 bit device */ |
| 76 | B64, /**< Accessing a bitmap on a 64 bit device */ |
| 77 | BMIN = BMALLOC, |
| 78 | BMAX = B64 |
| 79 | } bus_t; |
| 80 | |
| 81 | protected: |
| 82 | bus_t bus; |
| 83 | |
| 84 | unsigned memsize(void) const; |
| 85 | |
| 86 | public: |
| 87 | /** |
| 88 | * Create an object to reference the specified bitmap. |
| 89 | * @param addr of the bitmap in mapped memory. |
| 90 | * @param length of the bitmap being accessed in bits. |
| 91 | * @param size of the memory bus or manipulation to use. |
| 92 | */ |
| 93 | bitmap(void *addr, size_t length, bus_t size = B8); |
| 94 | |
| 95 | /** |
| 96 | * Create a bitmap to manipulate locally. This bitmap is created |
| 97 | * as part of the object itself, and uses the BMALLOC bus mode. |
| 98 | * @param length of bitmap to create in bits. |
| 99 | */ |
| 100 | bitmap(size_t length); |
| 101 | |
| 102 | /** |
| 103 | * Destroy bitmap manipulation object. If a bitmap was locally |
| 104 | * created with the alternate constructor, that bitmap will also be |
| 105 | * removed from memory. |
| 106 | */ |
| 107 | ~bitmap(); |
| 108 | |
| 109 | /** |
| 110 | * Clear (zero) all the bits in the bitmap. |
| 111 | */ |
| 112 | void clear(void); |
| 113 | |
| 114 | /** |
| 115 | * Get the value of a "bit" in the bitmap. |
| 116 | * @param offset to bit in map to get. |
| 117 | * @return true if bit is set. |
| 118 | */ |
| 119 | bool get(size_t offset) const; |
| 120 | |
| 121 | /** |
| 122 | * Set an individual bit in the bitmask. |
| 123 | * @param offset to bit in map to change. |
| 124 | * @param value to change specified bit to. |
| 125 | */ |
| 126 | void set(size_t offset, bool value); |
| 127 | }; |
| 128 | |
| 129 | END_NAMESPACE |
| 130 | |
| 131 | #endif |