501
502 #define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
503
504 // Signed variants of alignment helpers. There are two versions of each, a macro
505 // for use in places like enum definitions that require compile-time constant
506 // expressions and a function for all other places so as to get type checking.
507
508 // Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
509 // 'alignment' is an unsigned int and 'what' is a wider type. The & operation
510 // will widen the inverted mask, and not sign extend it, leading to a mask with
511 // zeros in the most significant bits. The use of align_mask_widened() solves
512 // this problem.
513 #define align_mask(alignment) ((alignment) - 1)
514 #define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
515 #define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
516
517 #define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
518
519 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
520
521 #define is_aligned_(size, alignment) ((size) == (align_up_(size, alignment)))
522
523 // Temporary declaration until this file has been restructured.
524 template <typename T>
525 bool is_power_of_2_t(T x) {
526 return (x != T(0)) && ((x & (x - 1)) == T(0));
527 }
528
529 // Helpers to align sizes and check for alignment
530
531 template <typename T, typename A>
532 inline T align_up(T size, A alignment) {
533 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
534
535 T ret = align_up_(size, alignment);
536 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
537
538 return ret;
539 }
540
541 template <typename T, typename A>
|
501
502 #define ARENA_AMALLOC_ALIGNMENT (2*BytesPerWord)
503
504 // Signed variants of alignment helpers. There are two versions of each, a macro
505 // for use in places like enum definitions that require compile-time constant
506 // expressions and a function for all other places so as to get type checking.
507
508 // Using '(what) & ~align_mask(alignment)' to align 'what' down is broken when
509 // 'alignment' is an unsigned int and 'what' is a wider type. The & operation
510 // will widen the inverted mask, and not sign extend it, leading to a mask with
511 // zeros in the most significant bits. The use of align_mask_widened() solves
512 // this problem.
513 #define align_mask(alignment) ((alignment) - 1)
514 #define widen_to_type_of(what, type_carrier) (true ? (what) : (type_carrier))
515 #define align_mask_widened(alignment, type_carrier) widen_to_type_of(align_mask(alignment), (type_carrier))
516
517 #define align_down_(size, alignment) ((size) & ~align_mask_widened((alignment), (size)))
518
519 #define align_up_(size, alignment) (align_down_((size) + align_mask(alignment), (alignment)))
520
521 #define is_aligned_(size, alignment) ((size) == (align_up_((size), (alignment))))
522
523 // Temporary declaration until this file has been restructured.
524 template <typename T>
525 bool is_power_of_2_t(T x) {
526 return (x != T(0)) && ((x & (x - 1)) == T(0));
527 }
528
529 // Helpers to align sizes and check for alignment
530
531 template <typename T, typename A>
532 inline T align_up(T size, A alignment) {
533 assert(is_power_of_2_t(alignment), "must be a power of 2: " UINT64_FORMAT, (uint64_t)alignment);
534
535 T ret = align_up_(size, alignment);
536 assert(is_aligned_(ret, alignment), "must be aligned: " UINT64_FORMAT, (uint64_t)ret);
537
538 return ret;
539 }
540
541 template <typename T, typename A>
|