|
31 | 31 |
|
32 | 32 | MU_DEFINE_ENUM(PAGED_SPARSE_ARRAY_ALLOCATE_RESULT, PAGED_SPARSE_ARRAY_ALLOCATE_RESULT_VALUES) |
33 | 33 |
|
| 34 | +/*result codes for PAGED_SPARSE_ARRAY_GET*/ |
| 35 | +#define PAGED_SPARSE_ARRAY_GET_RESULT_VALUES \ |
| 36 | + PAGED_SPARSE_ARRAY_GET_OK, \ |
| 37 | + PAGED_SPARSE_ARRAY_GET_INVALID_ARGS, \ |
| 38 | + PAGED_SPARSE_ARRAY_GET_NOT_ALLOCATED |
| 39 | + |
| 40 | +MU_DEFINE_ENUM(PAGED_SPARSE_ARRAY_GET_RESULT, PAGED_SPARSE_ARRAY_GET_RESULT_VALUES) |
| 41 | + |
34 | 42 | /*PAGED_SPARSE_ARRAY is backed by a THANDLE build on the structure below*/ |
35 | 43 | #define PAGED_SPARSE_ARRAY_STRUCT_TYPE_NAME_TAG(T) MU_C2(PAGED_SPARSE_ARRAY_TYPEDEF_NAME(T), _TAG) |
36 | 44 |
|
@@ -109,7 +117,7 @@ struct PAGED_SPARSE_ARRAY_STRUCT_TYPE_NAME_TAG(T) |
109 | 117 |
|
110 | 118 | /*introduces a function declaration for paged_sparse_array_get*/ |
111 | 119 | #define PAGED_SPARSE_ARRAY_LL_GET_DECLARE(C, T) \ |
112 | | - MOCKABLE_FUNCTION(, T*, PAGED_SPARSE_ARRAY_LL_GET(C), PAGED_SPARSE_ARRAY_LL(T), paged_sparse_array, uint64_t, index); |
| 120 | + MOCKABLE_FUNCTION(, PAGED_SPARSE_ARRAY_GET_RESULT, PAGED_SPARSE_ARRAY_LL_GET(C), PAGED_SPARSE_ARRAY_LL(T), paged_sparse_array, uint64_t, index, T**, item); |
113 | 121 |
|
114 | 122 | /*helper to compute bitmap size in bytes (1 bit per element, rounded up to nearest byte)*/ |
115 | 123 | #define PAGED_SPARSE_ARRAY_BITMAP_SIZE(page_size) (((page_size) + 7) / 8) |
@@ -352,48 +360,46 @@ void PAGED_SPARSE_ARRAY_LL_RELEASE(C)(PAGED_SPARSE_ARRAY_LL(T) paged_sparse_arra |
352 | 360 | } |
353 | 361 |
|
354 | 362 | #define PAGED_SPARSE_ARRAY_LL_GET_DEFINE(C, T) \ |
355 | | -T* PAGED_SPARSE_ARRAY_LL_GET(C)(PAGED_SPARSE_ARRAY_LL(T) paged_sparse_array, uint64_t index) \ |
| 363 | +PAGED_SPARSE_ARRAY_GET_RESULT PAGED_SPARSE_ARRAY_LL_GET(C)(PAGED_SPARSE_ARRAY_LL(T) paged_sparse_array, uint64_t index, T** item) \ |
356 | 364 | { \ |
357 | | - T* result; \ |
358 | | - /* Codes_SRS_PAGED_SPARSE_ARRAY_88_034: [ If paged_sparse_array is NULL, PAGED_SPARSE_ARRAY_GET(T) shall fail and return NULL. ]*/ \ |
359 | | - if (paged_sparse_array == NULL) \ |
360 | | - { \ |
361 | | - LogError("Invalid arguments: PAGED_SPARSE_ARRAY(" MU_TOSTRING(T) ") paged_sparse_array=%p", paged_sparse_array); \ |
362 | | - } \ |
363 | | - /* Codes_SRS_PAGED_SPARSE_ARRAY_88_035: [ If index is greater than or equal to max_size, PAGED_SPARSE_ARRAY_GET(T) shall fail and return NULL. ]*/ \ |
364 | | - else if (index >= paged_sparse_array->max_size) \ |
365 | | - { \ |
366 | | - LogError("Invalid arguments: uint64_t index=%" PRIu64 " out of bound, max_size=%" PRIu64 "", index, paged_sparse_array->max_size); \ |
367 | | - } \ |
| 365 | + PAGED_SPARSE_ARRAY_GET_RESULT result; \ |
| 366 | + if ( \ |
| 367 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_034: [ If paged_sparse_array is NULL, PAGED_SPARSE_ARRAY_GET(T) shall fail and return PAGED_SPARSE_ARRAY_GET_INVALID_ARGS. ]*/ \ |
| 368 | + paged_sparse_array == NULL || \ |
| 369 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_045: [ If item is NULL, PAGED_SPARSE_ARRAY_GET(T) shall fail and return PAGED_SPARSE_ARRAY_GET_INVALID_ARGS. ]*/ \ |
| 370 | + item == NULL || \ |
| 371 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_035: [ If index is greater than or equal to max_size, PAGED_SPARSE_ARRAY_GET(T) shall fail and return PAGED_SPARSE_ARRAY_GET_INVALID_ARGS. ]*/ \ |
| 372 | + index >= paged_sparse_array->max_size) \ |
| 373 | + { \ |
| 374 | + LogError("Invalid arguments: PAGED_SPARSE_ARRAY(" MU_TOSTRING(T) ") paged_sparse_array=%p, item=%p, uint64_t index=%" PRIu64, paged_sparse_array, item, index); \ |
| 375 | + result = PAGED_SPARSE_ARRAY_GET_INVALID_ARGS; \ |
| 376 | + } \ |
368 | 377 | else \ |
369 | 378 | { \ |
370 | 379 | /* Codes_SRS_PAGED_SPARSE_ARRAY_88_036: [ PAGED_SPARSE_ARRAY_GET(T) shall compute the page index as index / page_size. ]*/ \ |
371 | | - uint64_t page_index = index / paged_sparse_array->page_size; \ |
372 | | - \ |
373 | | - /* Codes_SRS_PAGED_SPARSE_ARRAY_88_037: [ If the page is not allocated, PAGED_SPARSE_ARRAY_GET(T) shall fail and return NULL. ]*/ \ |
374 | | - if (paged_sparse_array->pages[page_index] == NULL) \ |
| 380 | + uint64_t page_index = index / paged_sparse_array->page_size; \ |
| 381 | + \ |
| 382 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_037: [ If the page is not allocated, PAGED_SPARSE_ARRAY_GET(T) shall return PAGED_SPARSE_ARRAY_GET_NOT_ALLOCATED. ]*/ \ |
| 383 | + if (paged_sparse_array->pages[page_index] == NULL) \ |
375 | 384 | { \ |
376 | | - LogError("Page at page_index=%" PRIu64 " is not allocated", page_index); \ |
| 385 | + result = PAGED_SPARSE_ARRAY_GET_NOT_ALLOCATED; \ |
377 | 386 | } \ |
378 | 387 | else \ |
379 | 388 | { \ |
380 | | - uint64_t index_in_page = index % paged_sparse_array->page_size; \ |
381 | | - /* Codes_SRS_PAGED_SPARSE_ARRAY_88_038: [ If the element at index is not allocated, PAGED_SPARSE_ARRAY_GET(T) shall fail and return NULL. ]*/ \ |
382 | | - if (!PAGED_SPARSE_ARRAY_IS_ALLOCATED(PAGED_SPARSE_ARRAY_GET_BITMAP(paged_sparse_array->pages[page_index], paged_sparse_array->page_size), index_in_page)) \ |
| 389 | + uint64_t index_in_page = index % paged_sparse_array->page_size; \ |
| 390 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_038: [ If the element at index is not allocated, PAGED_SPARSE_ARRAY_GET(T) shall return PAGED_SPARSE_ARRAY_GET_NOT_ALLOCATED. ]*/ \ |
| 391 | + if (!PAGED_SPARSE_ARRAY_IS_ALLOCATED(PAGED_SPARSE_ARRAY_GET_BITMAP(paged_sparse_array->pages[page_index], paged_sparse_array->page_size), index_in_page)) \ |
383 | 392 | { \ |
384 | | - LogError("Element at index=%" PRIu64 " is not allocated", index); \ |
| 393 | + result = PAGED_SPARSE_ARRAY_GET_NOT_ALLOCATED; \ |
385 | 394 | } \ |
386 | 395 | else \ |
387 | 396 | { \ |
388 | | - /* Codes_SRS_PAGED_SPARSE_ARRAY_88_039: [ PAGED_SPARSE_ARRAY_GET(T) shall return a pointer to the element at index. ]*/ \ |
389 | | - result = &paged_sparse_array->pages[page_index]->items[index_in_page]; \ |
390 | | - goto all_ok; \ |
| 397 | + /* Codes_SRS_PAGED_SPARSE_ARRAY_88_039: [ PAGED_SPARSE_ARRAY_GET(T) shall store in item a pointer to the element at index and return PAGED_SPARSE_ARRAY_GET_OK. ]*/ \ |
| 398 | + *item = &paged_sparse_array->pages[page_index]->items[index_in_page]; \ |
| 399 | + result = PAGED_SPARSE_ARRAY_GET_OK; \ |
391 | 400 | } \ |
392 | 401 | } \ |
393 | 402 | } \ |
394 | | - result = NULL; \ |
395 | | -all_ok: \ |
396 | 403 | return result; \ |
397 | 404 | } |
398 | | - |
399 | 405 | #endif /*PAGED_SPARSE_ARRAY_LL_H*/ |
0 commit comments