Changeset 86733f3 in mainline


Ignore:
Timestamp:
2013-09-10T17:44:11Z (11 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
b0c2075
Parents:
65f77f4
Message:

implement bitmap allocation and deallocation
(this is a prototype implementation still lacking many optimizations, e.g. the 2nd level bitmap)

Location:
kernel/generic
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/adt/bitmap.h

    r65f77f4 r86733f3  
    9090extern void bitmap_clear_range(bitmap_t *, size_t, size_t);
    9191
    92 extern int bitmap_find_range(bitmap_t *, size_t, size_t, size_t);
    9392extern int bitmap_allocate_range(bitmap_t *, size_t, size_t, size_t, size_t *);
    9493extern void bitmap_free_range(bitmap_t *, size_t, size_t);
  • kernel/generic/src/adt/bitmap.c

    r65f77f4 r86733f3  
    280280}
    281281
     282static int constraint_satisfy(size_t index, size_t base, size_t constraint)
     283{
     284        return (((base + index) & constraint) == 0);
     285}
     286
     287/** Find a continuous zero bit range
     288 *
     289 * Find a continuous zero bit range in the bitmap. The address
     290 * computed as the sum of the index of the first zero bit and
     291 * the base argument needs to be compliant with the constraint
     292 * (those bits that are set in the constraint cannot be set in
     293 * the address).
     294 *
     295 * If the index argument is non-NULL, the continuous zero range
     296 * is set and the index of the first bit is stored to index.
     297 * Otherwise the bitmap stays untouched.
     298 *
     299 * @param bitmap     Bitmap structure.
     300 * @param count      Number of continuous zero bits to find.
     301 * @param base       Address of the first bit in the bitmap.
     302 * @param constraint Constraint for the address of the first zero bit.
     303 * @param index      Place to store the index of the first zero
     304 *                   bit. Can be NULL (in which case the bitmap
     305 *                   is not modified).
     306 *
     307 * @return Non-zero if a continuous range of zero bits satisfying
     308 *         the constraint has been found.
     309 * @return Zero otherwise.
     310 *
     311 */
     312int bitmap_allocate_range(bitmap_t *bitmap, size_t count, size_t base,
     313    size_t constraint, size_t *index)
     314{
     315                if (count == 0)
     316                return false;
     317       
     318        /*
     319         * This is a trivial implementation that should be
     320         * optimized further.
     321         */
     322       
     323        for (size_t i = 0; i < bitmap->elements; i++) {
     324                if (!constraint_satisfy(i, base, constraint))
     325                        continue;
     326               
     327                if (!bitmap_get(bitmap, i)) {
     328                        bool continuous = true;
     329                       
     330                        for (size_t j = 1; j < count; j++) {
     331                                if ((i + j >= bitmap->elements) ||
     332                                    (bitmap_get(bitmap, i + j))) {
     333                                        continuous = false;
     334                                        break;
     335                                }
     336                        }
     337                       
     338                        if (continuous) {
     339                                if (index != NULL) {
     340                                        bitmap_set_range(bitmap, i, count);
     341                                        *index = i;
     342                                }
     343                               
     344                                return true;
     345                        }
     346                }
     347               
     348        }
     349       
     350        return false;
     351}
     352
     353/** Clear range of set bits.
     354 *
     355 * This is essentially bitmap_clear_range(), but it also
     356 * checks whether all the cleared bits are actually set.
     357 *
     358 * @param bitmap Bitmap structure.
     359 * @param start  Starting bit.
     360 * @param count  Number of bits to clear.
     361 *
     362 */
     363void bitmap_free_range(bitmap_t *bitmap, size_t start, size_t count)
     364{
     365        /*
     366         * This is a trivial implementation that should be
     367         * optimized further.
     368         */
     369       
     370        for (size_t i = 0; i < count; i++) {
     371                if (!bitmap_get(bitmap, start + i))
     372                        panic("Freeing a bitmap range that is not set");
     373        }
     374       
     375        bitmap_clear_range(bitmap, start, count);
     376}
     377
    282378/** @}
    283379 */
Note: See TracChangeset for help on using the changeset viewer.