Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
SkMallocTest.cpp File Reference
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkMalloc.h"
#include "tests/Test.h"
#include <cstddef>
#include <cstdint>

Go to the source code of this file.

Functions

 DEF_TEST (SkFree_SafeToPassNull, reporter)
 
 DEF_TEST (SkCalloc_DataIsZeroInitializedAndWriteable, reporter)
 
 DEF_TEST (SkMalloc_DataIsWriteable, reporter)
 
 DEF_TEST (SkRealloc_DataIsWriteableAndEventuallyFreed, reporter)
 

Function Documentation

◆ DEF_TEST() [1/4]

DEF_TEST ( SkCalloc_DataIsZeroInitializedAndWriteable  ,
reporter   
)

Definition at line 20 of file SkMallocTest.cpp.

20 {
21 constexpr size_t alloc_count = 100;
22
23 uint8_t* bytes = (uint8_t*) sk_calloc_throw(alloc_count); // provide num bytes directly
24 SkASSERT_RELEASE(bytes != nullptr);
25 for (size_t i = 0; i < alloc_count; i++) {
26 REPORTER_ASSERT(reporter, bytes[i] == 0);
27 bytes[i] = (uint8_t)i;
28 }
29 sk_free(bytes);
30
31 int32_t* ints = (int32_t*) sk_calloc_throw(alloc_count, sizeof(int32_t)); // count + elem size
32 SkASSERT_RELEASE(ints != nullptr);
33 for (size_t i = 0; i < alloc_count; i++) {
34 REPORTER_ASSERT(reporter, ints[i] == 0);
35 ints[i] = (int32_t)i;
36 }
37 sk_free(ints);
38}
reporter
#define SkASSERT_RELEASE(cond)
Definition SkAssert.h:100
static void * sk_calloc_throw(size_t size)
Definition SkMalloc.h:71
SK_API void sk_free(void *)
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286

◆ DEF_TEST() [2/4]

DEF_TEST ( SkFree_SafeToPassNull  ,
reporter   
)

Definition at line 15 of file SkMallocTest.cpp.

15 {
16 // This test passes by not crashing
17 sk_free(nullptr);
18}

◆ DEF_TEST() [3/4]

DEF_TEST ( SkMalloc_DataIsWriteable  ,
reporter   
)

Definition at line 40 of file SkMallocTest.cpp.

40 {
41 constexpr size_t alloc_count = 100;
42
43 uint8_t* bytes = (uint8_t*) sk_malloc_throw(alloc_count); // provide num bytes directly
44 SkASSERT_RELEASE(bytes != nullptr);
45 for (size_t i = 0; i < alloc_count; i++) {
46 bytes[i] = (uint8_t)i;
47 }
48 sk_free(bytes);
49
50 int32_t* ints = (int32_t*) sk_malloc_throw(alloc_count, sizeof(int32_t)); // count + elem size
51 SkASSERT_RELEASE(ints != nullptr);
52 for (size_t i = 0; i < alloc_count; i++) {
53 ints[i] = (int32_t)i;
54 }
55 sk_free(ints);
56}
static void * sk_malloc_throw(size_t size)
Definition SkMalloc.h:67

◆ DEF_TEST() [4/4]

DEF_TEST ( SkRealloc_DataIsWriteableAndEventuallyFreed  ,
reporter   
)

Definition at line 58 of file SkMallocTest.cpp.

58 {
59 // Calling sk_realloc_throw with null should be treated as if it was sk_malloc_throw
60 uint8_t* bytes = (uint8_t*) sk_realloc_throw(nullptr, 10);
61 SkASSERT_RELEASE(bytes != nullptr);
62 // Make sure those 10 bytes are writeable
63 for (size_t i = 0; i < 10; i++) {
64 bytes[i] = (uint8_t)i;
65 }
66
67 // Make it smaller
68 bytes = (uint8_t*) sk_realloc_throw(bytes, 5);
69 SkASSERT_RELEASE(bytes != nullptr);
70 // Make sure those 5 bytes are still writeable and contain the previous values
71 for (int i = 0; i < 5; i++) {
72 REPORTER_ASSERT(reporter, bytes[i] == i, "bytes[%d] != %d", i, i);
73 bytes[i] = (uint8_t)i + 17;
74 }
75
76 // Make it bigger
77 bytes = (uint8_t*) sk_realloc_throw(bytes, 20, sizeof(uint8_t)); // count + elem size
78 SkASSERT_RELEASE(bytes != nullptr);
79 // Make sure the first 5 bytes are still writeable and contain the previous values
80 for (int i = 0; i < 5; i++) {
81 REPORTER_ASSERT(reporter, bytes[i] == (i + 17), "bytes[%d] != %d", i, i+17);
82 bytes[i] = (uint8_t)i + 43;
83 }
84 // The next 15 bytes are uninitialized, so just make sure we can write to them.
85 for (int i = 5; i < 20; i++) {
86 bytes[i] = (uint8_t)i + 43;
87 }
88
89 // This should free the memory and return nullptr.
90 bytes = (uint8_t*) sk_realloc_throw(bytes, 0);
91 REPORTER_ASSERT(reporter, bytes == nullptr);
92 // We run our tests with LeakSanitizer, so if bytes is *not* freed, we should see a failure.
93}
SK_API void * sk_realloc_throw(void *buffer, size_t size)