bh_bitmap.c 758 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_bitmap.h"
  6. bh_bitmap *
  7. bh_bitmap_new(uintptr_t begin_index, unsigned bitnum)
  8. {
  9. bh_bitmap *bitmap;
  10. uint32 bitmap_size = (bitnum + 7) / 8;
  11. uint32 total_size = offsetof(bh_bitmap, map) + bitmap_size;
  12. if (bitnum > UINT32_MAX - 7 || total_size < offsetof(bh_bitmap, map)
  13. || (total_size - offsetof(bh_bitmap, map)) != bitmap_size) {
  14. return NULL; /* integer overflow */
  15. }
  16. if ((bitmap = BH_MALLOC(total_size)) != NULL) {
  17. memset(bitmap, 0, total_size);
  18. bitmap->begin_index = begin_index;
  19. bitmap->end_index = begin_index + bitnum;
  20. }
  21. return bitmap;
  22. }