Skip to content

Commit 87dfd41

Browse files
authoredMay 23, 2020
Improve code quality for alloc_size (#196)
* Improve code quality for alloc_size * Made error noreturn. * Update docs. * Move annoation
1 parent fbbc2dd commit 87dfd41

8 files changed

+25
-16
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ your system.
160160
The PAL must implement the following methods:
161161

162162
```c++
163-
void error(const char* const str) noexcept;
163+
[[noreturn]] void error(const char* const str) noexcept;
164164
```
165165
Report a fatal error and exit.
166166

‎src/ds/defines.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
namespace snmalloc
3838
{
3939
// Forwards reference so that the platform can define how to handle errors.
40-
void error(const char* const str);
40+
[[noreturn]] void error(const char* const str);
4141
} // namespace snmalloc
4242

4343
#define TOSTRING(expr) TOSTRING2(expr)

‎src/mem/alloc.h

+17-8
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,19 @@ namespace snmalloc
453453
#endif
454454
}
455455

456-
static size_t alloc_size(const void* p)
456+
private:
457+
SNMALLOC_SLOW_PATH static size_t alloc_size_error()
458+
{
459+
error("Not allocated by this allocator");
460+
}
461+
462+
public:
463+
SNMALLOC_FAST_PATH static size_t alloc_size(const void* p)
457464
{
458465
// This must be called on an external pointer.
459466
size_t size = ChunkMap::get(address_cast(p));
460467

461-
if (size == 0)
462-
{
463-
error("Not allocated by this allocator");
464-
}
465-
else if (size == CMSuperslab)
468+
if (likely(size == CMSuperslab))
466469
{
467470
Superslab* super = Superslab::get(p);
468471

@@ -473,15 +476,21 @@ namespace snmalloc
473476

474477
return sizeclass_to_size(meta.sizeclass);
475478
}
476-
else if (size == CMMediumslab)
479+
480+
if (likely(size == CMMediumslab))
477481
{
478482
Mediumslab* slab = Mediumslab::get(p);
479483
// Reading a remote sizeclass won't fail, since the other allocator
480484
// can't reuse the slab, as we have no yet deallocated this pointer.
481485
return sizeclass_to_size(slab->get_sizeclass());
482486
}
483487

484-
return 1ULL << size;
488+
if (likely(size != 0))
489+
{
490+
return 1ULL << size;
491+
}
492+
493+
return alloc_size_error();
485494
}
486495

487496
size_t get_id()

‎src/pal/pal.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace snmalloc
4949
DefaultPal;
5050
#endif
5151

52-
SNMALLOC_SLOW_PATH inline void error(const char* const str)
52+
[[noreturn]] SNMALLOC_SLOW_PATH inline void error(const char* const str)
5353
{
5454
Pal::error(str);
5555
}

‎src/pal/pal_freebsd_kernel.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace snmalloc
2929
* PAL supports.
3030
*/
3131
static constexpr uint64_t pal_features = AlignedAllocation;
32-
void error(const char* const str)
32+
[[noreturn]] void error(const char* const str)
3333
{
3434
panic("snmalloc error: %s", str);
3535
}

‎src/pal/pal_open_enclave.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern "C" const void* __oe_get_heap_base();
66
extern "C" const void* __oe_get_heap_end();
77
extern "C" void* oe_memset_s(void* p, size_t p_size, int c, size_t size);
8-
extern "C" void oe_abort();
8+
extern "C" [[noreturn]] void oe_abort();
99

1010
namespace snmalloc
1111
{
@@ -19,7 +19,7 @@ namespace snmalloc
1919
* PAL supports.
2020
*/
2121
static constexpr uint64_t pal_features = 0;
22-
static void error(const char* const str)
22+
[[noreturn]] static void error(const char* const str)
2323
{
2424
UNUSED(str);
2525
oe_abort();

‎src/pal/pal_posix.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace snmalloc
5555
/**
5656
* Report a fatal error an exit.
5757
*/
58-
static void error(const char* const str) noexcept
58+
[[noreturn]] static void error(const char* const str) noexcept
5959
{
6060
puts(str);
6161
print_stack_trace();

‎src/pal/pal_windows.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace snmalloc
105105
low_memory_callbacks.register_notification(callback);
106106
}
107107

108-
static void error(const char* const str)
108+
[[noreturn]] static void error(const char* const str)
109109
{
110110
puts(str);
111111
fflush(stdout);

0 commit comments

Comments
 (0)
Please sign in to comment.