adds allocator interface and default implementation

Sun, 07 Feb 2021 12:20:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sun, 07 Feb 2021 12:20:07 +0100
changeset 391
f094a53c1178
parent 390
d345541018fa
child 392
c195c33de85d

adds allocator interface and default implementation

src/CMakeLists.txt file | annotate | diff | comparison | revisions
src/allocator.c file | annotate | diff | comparison | revisions
src/cx/allocator.h file | annotate | diff | comparison | revisions
test/CMakeLists.txt file | annotate | diff | comparison | revisions
test/test_allocator.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/CMakeLists.txt	Sat Feb 06 19:11:44 2021 +0100
     1.2 +++ b/src/CMakeLists.txt	Sun Feb 07 12:20:07 2021 +0100
     1.3 @@ -1,7 +1,9 @@
     1.4  set(sources
     1.5 +        allocator.c
     1.6          list.c
     1.7  )
     1.8  set(headers
     1.9 +        cx/allocator.h
    1.10          cx/list.h
    1.11  )
    1.12  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/allocator.c	Sun Feb 07 12:20:07 2021 +0100
     2.3 @@ -0,0 +1,56 @@
     2.4 +/*
     2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     2.6 + *
     2.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     2.8 + *
     2.9 + * Redistribution and use in source and binary forms, with or without
    2.10 + * modification, are permitted provided that the following conditions are met:
    2.11 + *
    2.12 + *   1. Redistributions of source code must retain the above copyright
    2.13 + *      notice, this list of conditions and the following disclaimer.
    2.14 + *
    2.15 + *   2. Redistributions in binary form must reproduce the above copyright
    2.16 + *      notice, this list of conditions and the following disclaimer in the
    2.17 + *      documentation and/or other materials provided with the distribution.
    2.18 + *
    2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    2.29 + * POSSIBILITY OF SUCH DAMAGE.
    2.30 + */
    2.31 +
    2.32 +#include "cx/allocator.h"
    2.33 +
    2.34 +#include <stdlib.h>
    2.35 +
    2.36 +struct cx_allocator_s cx_default_allocator = {
    2.37 +        cx_malloc_stdlib,
    2.38 +        cx_realloc_stdlib,
    2.39 +        cx_calloc_stdlib,
    2.40 +        cx_free_stdlib,
    2.41 +        NULL
    2.42 +};
    2.43 +CxAllocator cxDefaultAllocator = &cx_default_allocator;
    2.44 +
    2.45 +void* cx_malloc_stdlib(cx_allocator a, size_t n) {
    2.46 +    return malloc(n);
    2.47 +}
    2.48 +
    2.49 +void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n) {
    2.50 +    return realloc(mem, n);
    2.51 +}
    2.52 +
    2.53 +void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n) {
    2.54 +    return calloc(nelem, n);
    2.55 +}
    2.56 +
    2.57 +void cx_free_stdlib(cx_allocator a, void* mem) {
    2.58 +    free(mem);
    2.59 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/cx/allocator.h	Sun Feb 07 12:20:07 2021 +0100
     3.3 @@ -0,0 +1,61 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     3.8 + *
     3.9 + * Redistribution and use in source and binary forms, with or without
    3.10 + * modification, are permitted provided that the following conditions are met:
    3.11 + *
    3.12 + *   1. Redistributions of source code must retain the above copyright
    3.13 + *      notice, this list of conditions and the following disclaimer.
    3.14 + *
    3.15 + *   2. Redistributions in binary form must reproduce the above copyright
    3.16 + *      notice, this list of conditions and the following disclaimer in the
    3.17 + *      documentation and/or other materials provided with the distribution.
    3.18 + *
    3.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    3.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    3.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    3.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    3.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    3.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    3.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    3.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    3.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    3.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    3.29 + * POSSIBILITY OF SUCH DAMAGE.
    3.30 + */
    3.31 +
    3.32 +#ifndef UCX_ALLOCATOR_H
    3.33 +#define UCX_ALLOCATOR_H
    3.34 +
    3.35 +#include <stdlib.h>
    3.36 +
    3.37 +typedef void* cx_allocator;
    3.38 +typedef void*(*cx_malloc_func)(cx_allocator a, size_t n);
    3.39 +typedef void*(*cx_realloc_func)(cx_allocator a, void* mem, size_t n);
    3.40 +typedef void*(*cx_calloc_func)(cx_allocator a, size_t nelem, size_t n);
    3.41 +typedef void(*cx_free_func)(cx_allocator a, void* mem);
    3.42 +
    3.43 +void* cx_malloc_stdlib(cx_allocator a, size_t n);
    3.44 +void* cx_realloc_stdlib(cx_allocator a, void* mem, size_t n);
    3.45 +void* cx_calloc_stdlib(cx_allocator a, size_t nelem, size_t n);
    3.46 +void cx_free_stdlib(cx_allocator a, void* mem);
    3.47 +
    3.48 +struct cx_allocator_s {
    3.49 +    cx_malloc_func malloc;
    3.50 +    cx_realloc_func realloc;
    3.51 +    cx_calloc_func calloc;
    3.52 +    cx_free_func free;
    3.53 +    void* data;
    3.54 +};
    3.55 +typedef struct cx_allocator_s* CxAllocator;
    3.56 +
    3.57 +extern CxAllocator cxDefaultAllocator;
    3.58 +
    3.59 +#define cxMalloc(a, n) (a)->malloc((a), n)
    3.60 +#define cxRealloc(a, mem, n) (a)->realloc((a), mem, n)
    3.61 +#define cxCalloc(a, nelem, n) (a)->calloc((a), nelem, n)
    3.62 +#define cxFree(a, mem) (a)->free((a), mem)
    3.63 +
    3.64 +#endif //UCX_ALLOCATOR_H
     4.1 --- a/test/CMakeLists.txt	Sat Feb 06 19:11:44 2021 +0100
     4.2 +++ b/test/CMakeLists.txt	Sun Feb 07 12:20:07 2021 +0100
     4.3 @@ -1,6 +1,22 @@
     4.4 +message(CHECK_START "Searching for CUnit test framework")
     4.5  
     4.6 -add_executable(test_list
     4.7 -        test_list.c
     4.8 -)
     4.9 -target_link_libraries(test_list PRIVATE ucx_static)
    4.10 -add_test(NAME list COMMAND test_list WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
    4.11 +find_path(CUNIT_INCLUDE_DIR NAMES CUnit/CUnit.h)
    4.12 +find_library(CUNIT_LIBRARY NAMES cunit libcunit cunitlib)
    4.13 +include(FindPackageHandleStandardArgs)
    4.14 +find_package_handle_standard_args(CUnit DEFAULT_MSG CUNIT_LIBRARY CUNIT_INCLUDE_DIR)
    4.15 +
    4.16 +if(CUNIT_FOUND)
    4.17 +    message(CHECK_PASS "found: compiling tests.")
    4.18 +    set(TESTS
    4.19 +            test_allocator
    4.20 +            test_list
    4.21 +    )
    4.22 +
    4.23 +    foreach(test ${TESTS})
    4.24 +        add_executable(${test} ${test}.c)
    4.25 +        target_link_libraries(${test} PRIVATE ucx_static ${CUNIT_LIBRARY})
    4.26 +        add_test(NAME ${test} COMMAND ${test} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
    4.27 +    endforeach()
    4.28 +else()
    4.29 +    message(CHECK_FAIL "not found: unit tests will not be available.")
    4.30 +endif()
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test/test_allocator.c	Sun Feb 07 12:20:07 2021 +0100
     5.3 @@ -0,0 +1,94 @@
     5.4 +/*
     5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     5.6 + *
     5.7 + * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
     5.8 + *
     5.9 + * Redistribution and use in source and binary forms, with or without
    5.10 + * modification, are permitted provided that the following conditions are met:
    5.11 + *
    5.12 + *   1. Redistributions of source code must retain the above copyright
    5.13 + *      notice, this list of conditions and the following disclaimer.
    5.14 + *
    5.15 + *   2. Redistributions in binary form must reproduce the above copyright
    5.16 + *      notice, this list of conditions and the following disclaimer in the
    5.17 + *      documentation and/or other materials provided with the distribution.
    5.18 + *
    5.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    5.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    5.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    5.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    5.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    5.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    5.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    5.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    5.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    5.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    5.29 + * POSSIBILITY OF SUCH DAMAGE.
    5.30 + */
    5.31 +
    5.32 +#include "cx/allocator.h"
    5.33 +
    5.34 +#include <CUnit/Basic.h>
    5.35 +
    5.36 +void test_default_allocator_available(void) {
    5.37 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->malloc, cx_malloc_stdlib)
    5.38 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->realloc, cx_realloc_stdlib)
    5.39 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->calloc, cx_calloc_stdlib)
    5.40 +    CU_ASSERT_PTR_EQUAL(cxDefaultAllocator->free, cx_free_stdlib)
    5.41 +}
    5.42 +
    5.43 +void test_default_malloc(void) {
    5.44 +    void* test = cxMalloc(cxDefaultAllocator, 16);
    5.45 +    CU_ASSERT_PTR_NOT_NULL(test);
    5.46 +    free(test);
    5.47 +}
    5.48 +
    5.49 +void test_default_realloc(void) {
    5.50 +    void* test = calloc(8, 1);
    5.51 +    memcpy(test, "Test", 4);
    5.52 +    test = cxRealloc(cxDefaultAllocator, test, 16);
    5.53 +    CU_ASSERT_PTR_NOT_NULL(test);
    5.54 +    CU_ASSERT_STRING_EQUAL("Test", test)
    5.55 +    free(test);
    5.56 +}
    5.57 +
    5.58 +void test_default_calloc(void) {
    5.59 +    void* test = cxCalloc(cxDefaultAllocator, 8, 2);
    5.60 +    CU_ASSERT_PTR_NOT_NULL(test);
    5.61 +    free(test);
    5.62 +}
    5.63 +
    5.64 +void test_default_free(void) {
    5.65 +    CU_PASS("Testing standard free is not possible.")
    5.66 +}
    5.67 +
    5.68 +int main() {
    5.69 +    CU_pSuite suite = NULL;
    5.70 +
    5.71 +    if (CUE_SUCCESS != CU_initialize_registry()) {
    5.72 +        return CU_get_error();
    5.73 +    }
    5.74 +
    5.75 +    suite = CU_add_suite("stdlib allocator", NULL, NULL);
    5.76 +    if (NULL == suite) {
    5.77 +        CU_cleanup_registry();
    5.78 +        return CU_get_error();
    5.79 +    }
    5.80 +
    5.81 +    if (
    5.82 +            (NULL == CU_add_test(suite, "default allocator available", test_default_allocator_available)) ||
    5.83 +            (NULL == CU_add_test(suite, "test of malloc()", test_default_malloc)) ||
    5.84 +            (NULL == CU_add_test(suite, "test of realloc()", test_default_realloc)) ||
    5.85 +            (NULL == CU_add_test(suite, "test of realloc()", test_default_calloc)) ||
    5.86 +            (NULL == CU_add_test(suite, "test of free()", test_default_free))
    5.87 +        ) {
    5.88 +        CU_cleanup_registry();
    5.89 +        return CU_get_error();
    5.90 +    }
    5.91 +
    5.92 +    CU_basic_set_mode(CU_BRM_NORMAL);
    5.93 +    CU_basic_run_tests();
    5.94 +    CU_cleanup_registry();
    5.95 +
    5.96 +    return CU_get_error();
    5.97 +}

mercurial