diff --git a/common/array.c b/common/array.c index a837de793..e14effa0a 100644 --- a/common/array.c +++ b/common/array.c @@ -26,25 +26,24 @@ #include #include "py/runtime.h" #include "py/stackctrl.h" -#include "xalloc.h" #include "array.h" #define ARRAY_INIT_SIZE (4) // Size of one GC block. void array_alloc(array_t **a, array_dtor_t dtor) { - array_t *array = xalloc(sizeof(array_t)); + array_t *array = m_malloc(sizeof(array_t)); array->index = 0; array->length = ARRAY_INIT_SIZE; array->dtor = dtor; - array->data = xalloc(ARRAY_INIT_SIZE * sizeof(void *)); + array->data = m_malloc(ARRAY_INIT_SIZE * sizeof(void *)); *a = array; } void array_alloc_init(array_t **a, array_dtor_t dtor, int size) { - array_t *array = xalloc(sizeof(array_t)); + array_t *array = m_malloc(sizeof(array_t)); array->index = 0; array->length = size; array->dtor = dtor; - array->data = xalloc(size * sizeof(void *)); + array->data = m_malloc(size * sizeof(void *)); *a = array; } @@ -54,7 +53,7 @@ void array_clear(array_t *array) { array->dtor(array->data[i]); } } - xfree(array->data); + m_free(array->data); array->index = 0; array->length = 0; array->data = NULL; @@ -64,7 +63,7 @@ void array_clear(array_t *array) { void array_free(array_t *array) { array_clear(array); - xfree(array); + m_free(array); } int array_length(array_t *array) { @@ -78,7 +77,7 @@ void *array_at(array_t *array, int idx) { void array_push_back(array_t *array, void *element) { if (array->index == array->length) { array->length += ARRAY_INIT_SIZE; - array->data = xrealloc(array->data, array->length * sizeof(void *)); + array->data = m_realloc(array->data, array->length * sizeof(void *)); } array->data[array->index++] = element; } @@ -123,7 +122,7 @@ void array_resize(array_t *array, int num) { } // resize array array->length = num; - array->data = xrealloc(array->data, array->length * sizeof(void *)); + array->data = m_realloc(array->data, array->length * sizeof(void *)); } } } diff --git a/common/common.mk b/common/common.mk index a8704e8be..75d7daf59 100644 --- a/common/common.mk +++ b/common/common.mk @@ -39,7 +39,6 @@ COMMON_SRC_C += \ unaligned_memcpy.c \ usbdbg.c \ vospi.c \ - xalloc.c \ CFLAGS += -I$(TOP_DIR)/common OMV_FIRM_OBJ += $(addprefix $(BUILD)/common/, $(COMMON_SRC_C:.c=.o)) diff --git a/common/xalloc.c b/common/xalloc.c deleted file mode 100644 index 1f406f273..000000000 --- a/common/xalloc.c +++ /dev/null @@ -1,75 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (C) 2013-2024 OpenMV, LLC. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Memory allocation functions. - */ -#include -#include "py/runtime.h" -#include "py/gc.h" -#include "py/mphal.h" -#include "xalloc.h" - -NORETURN static void xalloc_fail(uint32_t size) { - mp_raise_msg_varg(&mp_type_MemoryError, - MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint) size); -} - -// returns null pointer without error if size==0 -void *xalloc(uint32_t size) { - void *mem = gc_alloc(size, false); - if (size && (mem == NULL)) { - xalloc_fail(size); - } - return mem; -} - -// returns null pointer without error if size==0 -void *xalloc_try_alloc(uint32_t size) { - return gc_alloc(size, false); -} - -// returns null pointer without error if size==0 -void *xalloc0(uint32_t size) { - void *mem = gc_alloc(size, false); - if (size && (mem == NULL)) { - xalloc_fail(size); - } - memset(mem, 0, size); - return mem; -} - -// returns without error if mem==null -void xfree(void *mem) { - gc_free(mem); -} - -// returns null pointer without error if size==0 -// allocs if mem==null and size!=0 -// frees if mem!=null and size==0 -void *xrealloc(void *mem, uint32_t size) { - mem = gc_realloc(mem, size, true); - if (size && (mem == NULL)) { - xalloc_fail(size); - } - return mem; -} diff --git a/common/xalloc.h b/common/xalloc.h deleted file mode 100644 index b8cb9c708..000000000 --- a/common/xalloc.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * SPDX-License-Identifier: MIT - * - * Copyright (C) 2013-2024 OpenMV, LLC. - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - * - * Memory allocation functions. - */ -#ifndef __XALLOC_H__ -#define __XALLOC_H__ -#include -void *xalloc(uint32_t size); -void *xalloc_try_alloc(uint32_t size); -void *xalloc0(uint32_t size); -void xfree(void *mem); -void *xrealloc(void *mem, uint32_t size); -#endif // __XALLOC_H__ diff --git a/lib/imlib/agast.c b/lib/imlib/agast.c index 31fb86328..a15627d76 100644 --- a/lib/imlib/agast.c +++ b/lib/imlib/agast.c @@ -13,7 +13,6 @@ */ #include "imlib.h" #if defined(IMLIB_ENABLE_AGAST) && !defined(OMV_NO_GPL) -#include "xalloc.h" #include "fb_alloc.h" #include "gc.h" @@ -44,7 +43,7 @@ static void nonmax_suppression(corner_t *corners, int num_corners, array_t *keyp static kp_t *alloc_keypoint(uint16_t x, uint16_t y, uint16_t score) { // Note must set keypoint descriptor to zeros - kp_t *kpt = xalloc0(sizeof *kpt); + kp_t *kpt = m_malloc0(sizeof *kpt); kpt->x = x; kpt->y = y; kpt->score = score; diff --git a/lib/imlib/blob.c b/lib/imlib/blob.c index b1deaa4a3..5501eaf38 100644 --- a/lib/imlib/blob.c +++ b/lib/imlib/blob.c @@ -68,7 +68,7 @@ static void bin_up(uint16_t *hist, uint16_t size, unsigned int max_size, uint16_ uint16_t bin_count = end - start + 1; // >= 1 *new_size = IM_MIN(max_size, bin_count); - *new_hist = xalloc0((*new_size) * sizeof(uint16_t)); + *new_hist = m_malloc0((*new_size) * sizeof(uint16_t)); float div_value = (*new_size) / ((float) bin_count); // Reversed so we can multiply below. for (int i = 0; i < bin_count; i++) { @@ -85,7 +85,7 @@ static void merge_bins(int b_dst_start, int b_dst_end, uint16_t **b_dst_hist, ui uint16_t bin_count = end - start + 1; // >= 1 uint16_t new_size = IM_MIN(max_size, bin_count); - uint16_t *new_hist = xalloc0(new_size * sizeof(uint16_t)); + uint16_t *new_hist = m_malloc0(new_size * sizeof(uint16_t)); float div_value = new_size / ((float) bin_count); // Reversed so we can multiply below. int b_dst_bin_count = b_dst_end - b_dst_start + 1; // >= 1 @@ -109,8 +109,8 @@ static void merge_bins(int b_dst_start, int b_dst_end, uint16_t **b_dst_hist, ui } } - xfree(*b_dst_hist); - xfree(*b_src_hist); + m_free(*b_dst_hist); + m_free(*b_src_hist); *b_dst_hist_len = new_size; (*b_dst_hist) = new_hist; @@ -462,10 +462,10 @@ void imlib_find_blobs(list_t *out, image_t *ptr, rectangle_t *roi, unsigned int list_push_back(out, &lnk_blob); } else { if (lnk_blob.x_hist_bins) { - xfree(lnk_blob.x_hist_bins); + m_free(lnk_blob.x_hist_bins); } if (lnk_blob.y_hist_bins) { - xfree(lnk_blob.y_hist_bins); + m_free(lnk_blob.y_hist_bins); } } } @@ -768,10 +768,10 @@ void imlib_find_blobs(list_t *out, image_t *ptr, rectangle_t *roi, unsigned int list_push_back(out, &lnk_blob); } else { if (lnk_blob.x_hist_bins) { - xfree(lnk_blob.x_hist_bins); + m_free(lnk_blob.x_hist_bins); } if (lnk_blob.y_hist_bins) { - xfree(lnk_blob.y_hist_bins); + m_free(lnk_blob.y_hist_bins); } } } @@ -1074,10 +1074,10 @@ void imlib_find_blobs(list_t *out, image_t *ptr, rectangle_t *roi, unsigned int list_push_back(out, &lnk_blob); } else { if (lnk_blob.x_hist_bins) { - xfree(lnk_blob.x_hist_bins); + m_free(lnk_blob.x_hist_bins); } if (lnk_blob.y_hist_bins) { - xfree(lnk_blob.y_hist_bins); + m_free(lnk_blob.y_hist_bins); } } } diff --git a/lib/imlib/bmp.c b/lib/imlib/bmp.c index f2af26a19..6ff7ef276 100644 --- a/lib/imlib/bmp.c +++ b/lib/imlib/bmp.c @@ -30,7 +30,6 @@ #include "py/obj.h" #include "py/runtime.h" -#include "xalloc.h" #include "file_utils.h" // This function inits the geometry values of an image (opens file). @@ -270,7 +269,7 @@ void bmp_read(image_t *img, const char *path) { file_open(&fp, path, true, FA_READ | FA_OPEN_EXISTING); bmp_read_geometry(&fp, img, path, &rs); if (!img->pixels) { - image_xalloc(img, img->w * img->h * img->bpp); + image_alloc(img, img->w * img->h * img->bpp); } bmp_read_pixels(&fp, img, img->h, &rs); file_close(&fp); diff --git a/lib/imlib/collections.c b/lib/imlib/collections.c index 424322fff..39373808e 100644 --- a/lib/imlib/collections.c +++ b/lib/imlib/collections.c @@ -209,7 +209,7 @@ void list_copy(list_t *dst, list_t *src) { void list_free(list_t *ptr) { for (list_lnk_t *i = ptr->head; i; ) { list_lnk_t *j = i->next; - xfree(i); + m_free(i); i = j; } } @@ -272,7 +272,7 @@ static void list_unlink(list_t *src, list_lnk_t *lnk) { } void list_insert(list_t *ptr, list_lnk_t *lnk, void *data) { - list_lnk_t *tmp = (list_lnk_t *) xalloc(sizeof(list_lnk_t) + ptr->data_len); + list_lnk_t *tmp = (list_lnk_t *) m_malloc(sizeof(list_lnk_t) + ptr->data_len); memcpy(tmp->data, data, ptr->data_len); list_link(ptr, lnk, tmp); } @@ -291,7 +291,7 @@ void list_remove(list_t *ptr, list_lnk_t *lnk, void *data) { } list_unlink(ptr, lnk); - xfree(lnk); + m_free(lnk); } void list_pop_front(list_t *ptr, void *data) { diff --git a/lib/imlib/dmtx.c b/lib/imlib/dmtx.c index 0799fc33d..ba6af4797 100644 --- a/lib/imlib/dmtx.c +++ b/lib/imlib/dmtx.c @@ -6077,7 +6077,7 @@ void imlib_find_datamatrices(list_t *out, image_t *ptr, rectangle_t *roi, int ef // Payload is NOT already null terminated. lnk_data.payload_len = message->outputIdx; - lnk_data.payload = xalloc(message->outputIdx); + lnk_data.payload = m_malloc(message->outputIdx); memcpy(lnk_data.payload, message->output, message->outputIdx); int rotate = fast_roundf((((2 * M_PI) + fast_atan2f(p[1].Y - p[0].Y, p[1].X - p[0].X)) * 180) / M_PI); diff --git a/lib/imlib/eye.c b/lib/imlib/eye.c index 712357d48..d7b85d082 100644 --- a/lib/imlib/eye.c +++ b/lib/imlib/eye.c @@ -24,7 +24,6 @@ * Pupil localization using image gradients. See Fabian Timm's paper for details. */ #include "imlib.h" -#include "xalloc.h" #include "fmath.h" static void find_gradients(image_t *src, array_t *gradients, int x_off, int y_off, int box_w, int box_h) { @@ -49,7 +48,7 @@ static void find_gradients(image_t *src, array_t *gradients, int x_off, int y_of float m = fast_sqrtf(vx * vx + vy * vy); if (m > 200) { - vec_t *v = xalloc(sizeof(vec_t)); + vec_t *v = m_malloc(sizeof(vec_t)); v->m = m; v->x = vx / m; v->y = vy / m; @@ -127,7 +126,7 @@ static void find_iris(image_t *src, array_t *gradients, int x_off, int y_off, in // This function should be called on an ROI detected with the eye Haar cascade. void imlib_find_iris(image_t *src, point_t *iris, rectangle_t *roi) { array_t *iris_gradients; - array_alloc(&iris_gradients, xfree); + array_alloc(&iris_gradients, m_free); // Tune these offsets to skip eyebrows and reduce window size int box_w = roi->w - ((int) (0.15f * roi->w)); diff --git a/lib/imlib/fast.c b/lib/imlib/fast.c index c585ceb00..583151c24 100644 --- a/lib/imlib/fast.c +++ b/lib/imlib/fast.c @@ -34,7 +34,6 @@ */ #include #include "imlib.h" -#include "xalloc.h" #include "fb_alloc.h" #include "gc.h" @@ -57,7 +56,7 @@ static void nonmax_suppression(corner_t *corners, int num_corners, array_t *keyp static kp_t *alloc_keypoint(uint16_t x, uint16_t y, uint16_t score) { // Note must set keypoint descriptor to zeros - kp_t *kpt = xalloc0(sizeof *kpt); + kp_t *kpt = m_malloc0(sizeof *kpt); kpt->x = x; kpt->y = y; kpt->score = score; diff --git a/lib/imlib/haar.c b/lib/imlib/haar.c index 20c2ce3e9..2d46d1c78 100644 --- a/lib/imlib/haar.c +++ b/lib/imlib/haar.c @@ -33,7 +33,6 @@ #include "extmod/vfs.h" #endif -#include "xalloc.h" #include "imlib.h" #ifdef IMLIB_ENABLE_FEATURES @@ -100,7 +99,7 @@ array_t *imlib_detect_objects(image_t *image, cascade_t *cascade, rectangle_t *r array_t *objects; // Allocate the objects array - array_alloc(&objects, xfree); + array_alloc(&objects, m_free); // Set cascade image pointers cascade->img = image; @@ -240,7 +239,7 @@ int imlib_load_cascade_from_file(cascade_t *cascade, const char *path) { mp_stream_read_exactly(file, &cascade->n_stages, sizeof(cascade->n_stages), &error); // Allocate stages array. - cascade->stages_array = xalloc(sizeof(int8_t) * cascade->n_stages); + cascade->stages_array = m_malloc(sizeof(int8_t) * cascade->n_stages); // Read number of features in each stages mp_stream_read_exactly(file, cascade->stages_array, cascade->n_stages, &error); @@ -256,11 +255,11 @@ int imlib_load_cascade_from_file(cascade_t *cascade, const char *path) { } // Alloc features thresh array, alpha1, alpha 2,rects weights and rects - cascade->stages_thresh_array = xalloc(sizeof(int16_t) * cascade->n_stages); - cascade->tree_thresh_array = xalloc(sizeof(int16_t) * cascade->n_features); - cascade->alpha1_array = xalloc(sizeof(int16_t) * cascade->n_features); - cascade->alpha2_array = xalloc(sizeof(int16_t) * cascade->n_features); - cascade->num_rectangles_array = xalloc(sizeof(int8_t) * cascade->n_features); + cascade->stages_thresh_array = m_malloc(sizeof(int16_t) * cascade->n_stages); + cascade->tree_thresh_array = m_malloc(sizeof(int16_t) * cascade->n_features); + cascade->alpha1_array = m_malloc(sizeof(int16_t) * cascade->n_features); + cascade->alpha2_array = m_malloc(sizeof(int16_t) * cascade->n_features); + cascade->num_rectangles_array = m_malloc(sizeof(int8_t) * cascade->n_features); // Read features thresh array, alpha1, alpha 2,rects weights and rects mp_stream_read_exactly(file, cascade->stages_thresh_array, sizeof(int16_t) * cascade->n_stages, &error); @@ -275,8 +274,8 @@ int imlib_load_cascade_from_file(cascade_t *cascade, const char *path) { } // Allocate weights and rectangles arrays. - cascade->weights_array = xalloc(cascade->n_rectangles); - cascade->rectangles_array = xalloc(cascade->n_rectangles * 4); + cascade->weights_array = m_malloc(cascade->n_rectangles); + cascade->rectangles_array = m_malloc(cascade->n_rectangles * 4); // Read rectangles weights and rectangles (number of rectangles * 4 points) mp_stream_read_exactly(file, cascade->weights_array, sizeof(int8_t) * cascade->n_rectangles, &error); diff --git a/lib/imlib/hog.c b/lib/imlib/hog.c index 571573460..1a9137964 100644 --- a/lib/imlib/hog.c +++ b/lib/imlib/hog.c @@ -29,7 +29,6 @@ #include #include "imlib.h" #include "fb_alloc.h" -#include "xalloc.h" #ifdef IMLIB_ENABLE_HOG #define N_BINS (9) @@ -145,7 +144,7 @@ void imlib_find_hog(image_t *src, rectangle_t *roi, int cell_size) { } } - xfree(gds); + m_free(gds); fb_free(); } #endif // IMLIB_ENABLE_HOG diff --git a/lib/imlib/imlib.c b/lib/imlib/imlib.c index 4a8f57a6b..b0591b38d 100644 --- a/lib/imlib/imlib.c +++ b/lib/imlib/imlib.c @@ -262,17 +262,17 @@ void rectangle_united(rectangle_t *dst, rectangle_t *src) { // Image Stuff // ///////////////// -void image_xalloc(image_t *img, size_t size) { +void image_alloc(image_t *img, size_t size) { // Round the size up to ensure that the allocation is a multiple of the alignment in bytes. // This ensures after address alignment that the data can be modified without affecting other cache lines. size = ((size + OMV_ALLOC_ALIGNMENT - 1) / OMV_ALLOC_ALIGNMENT) * OMV_ALLOC_ALIGNMENT; - img->_raw = xalloc(size + OMV_ALLOC_ALIGNMENT - 1); + img->_raw = m_malloc(size + OMV_ALLOC_ALIGNMENT - 1); // Offset the data pointer to ensure it is aligned. img->data = (void *) (((uintptr_t) img->_raw + OMV_ALLOC_ALIGNMENT - 1) & ~(OMV_ALLOC_ALIGNMENT - 1)); } -void image_xalloc0(image_t *img, size_t size) { - image_xalloc(img, size); +void image_alloc0(image_t *img, size_t size) { + image_alloc(img, size); memset(img->data, 0, size); } diff --git a/lib/imlib/imlib.h b/lib/imlib/imlib.h index b0d8a3afe..ad0b75cbb 100644 --- a/lib/imlib/imlib.h +++ b/lib/imlib/imlib.h @@ -38,13 +38,13 @@ #include "fb_alloc.h" #include "file_utils.h" #include "umm_malloc.h" -#include "xalloc.h" #include "array.h" #include "fmath.h" #include "collections.h" #include "imlib_config.h" #include "omv_boardconfig.h" #include "omv_common.h" +#include "py/runtime.h" // Enables 38 TensorFlow Lite operators. #define IMLIB_TF_DEFAULT (1) @@ -559,7 +559,7 @@ typedef struct image { int32_t w; int32_t h; PIXFORMAT_STRUCT; - // Keeps a reference to the GC block when used with image_xalloc/image_xalloc0. + // Keeps a reference to the GC block when used with image_alloc/image_alloc0. uint8_t *_raw; union { uint8_t *pixels; @@ -567,8 +567,8 @@ typedef struct image { }; } image_t; -void image_xalloc(image_t *img, size_t size); -void image_xalloc0(image_t *img, size_t size); +void image_alloc(image_t *img, size_t size); +void image_alloc0(image_t *img, size_t size); void image_init(image_t *ptr, int w, int h, pixformat_t pixfmt, uint32_t size, void *pixels); void image_copy(image_t *dst, image_t *src, bool deep); size_t image_line_size(image_t *ptr); diff --git a/lib/imlib/jpege.c b/lib/imlib/jpege.c index b6fdf945a..03f4f7a42 100644 --- a/lib/imlib/jpege.c +++ b/lib/imlib/jpege.c @@ -592,7 +592,7 @@ static int jpeg_check_highwater(jpeg_buf_t *jpeg_buf) { return 1; } jpeg_buf->length += 1024; - jpeg_buf->buf = xrealloc(jpeg_buf->buf, jpeg_buf->length); + jpeg_buf->buf = m_realloc(jpeg_buf->buf, jpeg_buf->length); } return 0; } @@ -605,7 +605,7 @@ static void jpeg_put_char(jpeg_buf_t *jpeg_buf, char c) { return; } jpeg_buf->length += 1024; - jpeg_buf->buf = xrealloc(jpeg_buf->buf, jpeg_buf->length); + jpeg_buf->buf = m_realloc(jpeg_buf->buf, jpeg_buf->length); } jpeg_buf->buf[jpeg_buf->idx++] = c; @@ -619,7 +619,7 @@ static void jpeg_put_bytes(jpeg_buf_t *jpeg_buf, const void *data, int size) { return; } jpeg_buf->length += 1024; - jpeg_buf->buf = xrealloc(jpeg_buf->buf, jpeg_buf->length); + jpeg_buf->buf = m_realloc(jpeg_buf->buf, jpeg_buf->length); } memcpy(jpeg_buf->buf + jpeg_buf->idx, data, size); @@ -1371,7 +1371,7 @@ void jpeg_read(image_t *img, const char *path) { jpeg_read_geometry(&fp, img, path, &rs); if (!img->pixels) { - image_xalloc(img, img->size); + image_alloc(img, img->size); } jpeg_read_pixels(&fp, img); diff --git a/lib/imlib/kmeans.c b/lib/imlib/kmeans.c index 29ee0c484..f057fd2bc 100644 --- a/lib/imlib/kmeans.c +++ b/lib/imlib/kmeans.c @@ -29,13 +29,12 @@ #include #include "imlib.h" #include "array.h" -#include "xalloc.h" extern uint32_t rng_randint(uint32_t min, uint32_t max); static cluster_t *cluster_alloc(int cx, int cy) { cluster_t *c = NULL; - c = xalloc(sizeof(*c)); + c = m_malloc(sizeof(*c)); if (c != NULL) { c->x = cx; c->y = cy; @@ -49,7 +48,7 @@ static cluster_t *cluster_alloc(int cx, int cy) { static void cluster_free(void *c) { cluster_t *cl = c; array_free(cl->points); - xfree(cl); + m_free(cl); } static void cluster_reset(array_t *clusters, array_t *points) { diff --git a/lib/imlib/lbp.c b/lib/imlib/lbp.c index 2d0f18989..3509ef6d9 100644 --- a/lib/imlib/lbp.c +++ b/lib/imlib/lbp.c @@ -30,7 +30,6 @@ #include #include "imlib.h" -#include "xalloc.h" #include "file_utils.h" #ifdef IMLIB_ENABLE_FIND_LBP @@ -72,7 +71,7 @@ uint8_t *imlib_lbp_desc(image_t *image, rectangle_t *roi) { int RX = roi->w / LBP_NUM_REGIONS; int RY = roi->h / LBP_NUM_REGIONS; uint8_t *data = image->data; - uint8_t *desc = xalloc0(LBP_DESC_SIZE); + uint8_t *desc = m_malloc0(LBP_DESC_SIZE); for (int y = roi->y; y < (roi->y + roi->h) - 3; y++) { int y_idx = ((y - roi->y) / RY) * LBP_NUM_REGIONS; @@ -116,13 +115,13 @@ int imlib_lbp_desc_load(FIL *fp, uint8_t **desc) { FRESULT res = FR_OK; *desc = NULL; - uint8_t *hist = xalloc(LBP_DESC_SIZE); + uint8_t *hist = m_malloc(LBP_DESC_SIZE); // Read descriptor res = file_ll_read(fp, hist, LBP_DESC_SIZE, &bytes); if (res != FR_OK || bytes != LBP_DESC_SIZE) { *desc = NULL; - xfree(hist); + m_free(hist); } else { *desc = hist; } diff --git a/lib/imlib/orb.c b/lib/imlib/orb.c index 562272d27..93674d820 100644 --- a/lib/imlib/orb.c +++ b/lib/imlib/orb.c @@ -37,7 +37,6 @@ #if defined(IMLIB_ENABLE_FIND_KEYPOINTS) #include "fmath.h" #include "arm_math.h" -#include "xalloc.h" #include "fb_alloc.h" #include "file_utils.h" @@ -372,7 +371,7 @@ static void image_scale(image_t *src, image_t *dst) { array_t *orb_find_keypoints(image_t *img, bool normalized, int threshold, float scale_factor, int max_keypoints, corner_detector_t corner_detector, rectangle_t *roi) { array_t *kpts; - array_alloc(&kpts, xfree); + array_alloc(&kpts, m_free); int octave = 1; int kpts_index = 0; @@ -778,7 +777,7 @@ int orb_load_descriptor(FIL *fp, array_t *kpts) { // Read keypoints for (int i = 0; i < kpts_size; i++) { - kp_t *kp = xalloc(sizeof(*kp)); + kp_t *kp = m_malloc(sizeof(*kp)); kp->matched = 0; // Read X diff --git a/lib/imlib/png.c b/lib/imlib/png.c index 0e2ce4f55..b20c04212 100644 --- a/lib/imlib/png.c +++ b/lib/imlib/png.c @@ -302,7 +302,7 @@ void png_read(image_t *img, const char *path) { png_read_geometry(&fp, img, path, &rs); if (!img->pixels) { - image_xalloc(img, img->size); + image_alloc(img, img->size); } png_read_pixels(&fp, img); diff --git a/lib/imlib/point.c b/lib/imlib/point.c index 04845f7e1..f6c97e5a4 100644 --- a/lib/imlib/point.c +++ b/lib/imlib/point.c @@ -24,10 +24,9 @@ * Point functions. */ #include "imlib.h" -#include "xalloc.h" point_t *point_alloc(int16_t x, int16_t y) { - point_t *p = xalloc(sizeof(point_t)); + point_t *p = m_malloc(sizeof(point_t)); p->x = x; p->y = y; return p; diff --git a/lib/imlib/ppm.c b/lib/imlib/ppm.c index 1e5a31e05..a2c61b691 100644 --- a/lib/imlib/ppm.c +++ b/lib/imlib/ppm.c @@ -31,7 +31,6 @@ #include "py/obj.h" #include "py/runtime.h" -#include "xalloc.h" #include "imlib.h" #include "file_utils.h" @@ -143,7 +142,7 @@ void ppm_read(image_t *img, const char *path) { ppm_read_geometry(&fp, img, path, &rs); if (!img->pixels) { - image_xalloc(img, img->w * img->h * img->bpp); + image_alloc(img, img->w * img->h * img->bpp); } ppm_read_pixels(&fp, img, img->h, &rs); file_close(&fp); diff --git a/lib/imlib/qrcode.c b/lib/imlib/qrcode.c index 939317ce3..d34bd373b 100644 --- a/lib/imlib/qrcode.c +++ b/lib/imlib/qrcode.c @@ -3016,7 +3016,7 @@ void imlib_find_qrcodes(list_t *out, image_t *ptr, rectangle_t *roi) // Payload is already null terminated. lnk_data.payload_len = data->payload_len; - lnk_data.payload = xalloc(data->payload_len); + lnk_data.payload = m_malloc(data->payload_len); memcpy(lnk_data.payload, data->payload, data->payload_len); lnk_data.version = data->version; diff --git a/lib/imlib/rectangle.c b/lib/imlib/rectangle.c index 0d467c3c2..452cdae68 100644 --- a/lib/imlib/rectangle.c +++ b/lib/imlib/rectangle.c @@ -25,10 +25,9 @@ */ #include "imlib.h" #include "array.h" -#include "xalloc.h" rectangle_t *rectangle_alloc(int16_t x, int16_t y, int16_t w, int16_t h) { - rectangle_t *r = xalloc(sizeof(rectangle_t)); + rectangle_t *r = m_malloc(sizeof(rectangle_t)); r->x = x; r->y = y; r->w = w; @@ -87,8 +86,8 @@ static void rectangle_div(rectangle_t *r, int c) { } array_t *rectangle_merge(array_t *rectangles) { - array_t *objects; array_alloc(&objects, xfree); - array_t *overlap; array_alloc(&overlap, xfree); + array_t *objects; array_alloc(&objects, m_free); + array_t *overlap; array_alloc(&overlap, m_free); /* merge overlapping detections */ while (array_length(rectangles)) { /* check for overlapping detections */ @@ -104,7 +103,7 @@ array_t *rectangle_merge(array_t *rectangles) { for (int i = 0; i < count; i++) { rectangle_t *overlap_rect = (rectangle_t *) array_pop_back(overlap); rectangle_add(rect, overlap_rect); - xfree(overlap_rect); + m_free(overlap_rect); } /* average the overlapping detections */ rectangle_div(rect, count + 1); diff --git a/lib/imlib/selective_search.c b/lib/imlib/selective_search.c index c047dadfc..2e859de62 100644 --- a/lib/imlib/selective_search.c +++ b/lib/imlib/selective_search.c @@ -29,7 +29,6 @@ #include #include "imlib.h" #include "fb_alloc.h" -#include "xalloc.h" #ifdef IMLIB_ENABLE_SELECTIVE_SEARCH #define THRESHOLD(size, c) (c / size) @@ -235,7 +234,7 @@ array_t *imlib_selective_search(image_t *src, float t, int min_size, float a1, f // Region proposals array array_t *proposals; - array_alloc(&proposals, xfree); + array_alloc(&proposals, m_free); universe *u = universe_create(width * height); edge *edges = (edge *) fb_alloc(width * height * sizeof(edge) * 4, FB_ALLOC_NO_HINT); diff --git a/lib/imlib/template.c b/lib/imlib/template.c index 92cccac16..204fb26bf 100644 --- a/lib/imlib/template.c +++ b/lib/imlib/template.c @@ -33,7 +33,6 @@ #include #include "imlib.h" -#include "xalloc.h" static void set_dsp(int cx, int cy, point_t *pts, bool sdsp, int step) { if (sdsp) { diff --git a/lib/imlib/zbar.c b/lib/imlib/zbar.c index 7555ffff3..62610cc16 100644 --- a/lib/imlib/zbar.c +++ b/lib/imlib/zbar.c @@ -8787,7 +8787,7 @@ void imlib_find_barcodes(list_t *out, image_t *ptr, rectangle_t *roi) // Payload is already null terminated. lnk_data.payload_len = zbar_symbol_get_data_length(symbol); - lnk_data.payload = xalloc(zbar_symbol_get_data_length(symbol)); + lnk_data.payload = m_malloc(zbar_symbol_get_data_length(symbol)); memcpy(lnk_data.payload, zbar_symbol_get_data(symbol), zbar_symbol_get_data_length(symbol)); switch (zbar_symbol_get_type(symbol)) { diff --git a/modules/py_csi.c b/modules/py_csi.c index 2307c5aab..a1451ce91 100644 --- a/modules/py_csi.c +++ b/modules/py_csi.c @@ -41,7 +41,6 @@ #include "omv_gpio.h" #include "imlib.h" -#include "xalloc.h" #include "py_assert.h" #include "py_image.h" #if MICROPY_PY_IMU @@ -964,7 +963,7 @@ static mp_obj_t py_omv_csi_ioctl(size_t n_args, const mp_obj_t *args) { int command = mp_obj_get_int(args[1]); size_t data_len = mp_obj_get_int(args[2]); PY_ASSERT_TRUE_MSG(data_len > 0, "0 bytes transferred!"); - uint16_t *data = xalloc(data_len * sizeof(uint16_t)); + uint16_t *data = m_malloc(data_len * sizeof(uint16_t)); error = omv_csi_ioctl(csi, request, command, data, data_len); if (error == 0) { ret_obj = mp_obj_new_bytearray_by_ref(data_len * sizeof(uint16_t), data); diff --git a/modules/py_csi_ng.c b/modules/py_csi_ng.c index a189001b4..d961ce05a 100644 --- a/modules/py_csi_ng.c +++ b/modules/py_csi_ng.c @@ -41,7 +41,6 @@ #include "omv_gpio.h" #include "imlib.h" -#include "xalloc.h" #include "py_assert.h" #include "py_image.h" #if MICROPY_PY_IMU @@ -1028,7 +1027,7 @@ static mp_obj_t py_csi_ioctl(size_t n_args, const mp_obj_t *args) { int command = mp_obj_get_int(args[0]); size_t data_len = mp_obj_get_int(args[1]); PY_ASSERT_TRUE_MSG(data_len > 0, "0 bytes transferred!"); - uint16_t *data = xalloc(data_len * sizeof(uint16_t)); + uint16_t *data = m_malloc(data_len * sizeof(uint16_t)); error = omv_csi_ioctl(self->csi, request, command, data, data_len); if (error == 0) { ret_obj = mp_obj_new_bytearray_by_ref(data_len * sizeof(uint16_t), data); diff --git a/modules/py_fir.c b/modules/py_fir.c index edfda7e93..0fd73dbb0 100644 --- a/modules/py_fir.c +++ b/modules/py_fir.c @@ -416,7 +416,7 @@ mp_obj_t py_fir_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) ir_fresh_rate = 14 - __CLZ(__RBIT((ir_fresh_rate > 512) ? 512 : ((ir_fresh_rate < 1) ? 1 : ir_fresh_rate))); adc_resolution = ((adc_resolution > 18) ? 18 : ((adc_resolution < 15) ? 15 : adc_resolution)) - 15; - MP_STATE_PORT(fir_mlx_data) = xalloc(sizeof(paramsMLX90621)); + MP_STATE_PORT(fir_mlx_data) = m_malloc(sizeof(paramsMLX90621)); fir_sensor = FIR_MLX90621; FIR_MLX90621_RETRY: @@ -463,7 +463,7 @@ mp_obj_t py_fir_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) ir_fresh_rate = __CLZ(__RBIT((ir_fresh_rate > 64) ? 64 : ((ir_fresh_rate < 1) ? 1 : ir_fresh_rate))) + 1; adc_resolution = ((adc_resolution > 19) ? 19 : ((adc_resolution < 16) ? 16 : adc_resolution)) - 16; - MP_STATE_PORT(fir_mlx_data) = xalloc(sizeof(paramsMLX90640)); + MP_STATE_PORT(fir_mlx_data) = m_malloc(sizeof(paramsMLX90640)); fir_sensor = FIR_MLX90640; FIR_MLX90640_RETRY: @@ -509,7 +509,7 @@ mp_obj_t py_fir_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) ir_fresh_rate = __CLZ(__RBIT((ir_fresh_rate > 64) ? 64 : ((ir_fresh_rate < 1) ? 1 : ir_fresh_rate))) + 1; adc_resolution = ((adc_resolution > 19) ? 19 : ((adc_resolution < 16) ? 16 : adc_resolution)) - 16; - MP_STATE_PORT(fir_mlx_data) = xalloc(sizeof(paramsMLX90641)); + MP_STATE_PORT(fir_mlx_data) = m_malloc(sizeof(paramsMLX90641)); fir_sensor = FIR_MLX90641; FIR_MLX90641_RETRY: @@ -1023,7 +1023,7 @@ mp_obj_t py_fir_snapshot(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a if (args[ARG_copy_to_fb].u_bool) { py_helper_set_to_framebuffer(&dst_img); } else { - dst_img.data = xalloc(image_size(&dst_img)); + dst_img.data = m_malloc(image_size(&dst_img)); } float min = FLT_MAX; diff --git a/modules/py_helper.c b/modules/py_helper.c index ffa44b004..0bb779646 100644 --- a/modules/py_helper.c +++ b/modules/py_helper.c @@ -46,7 +46,7 @@ image_t *py_helper_arg_to_image(const mp_obj_t arg, uint32_t flags) { #if defined(IMLIB_ENABLE_IMAGE_FILE_IO) const char *path = mp_obj_str_get_str(arg); FIL fp; - image = xalloc(sizeof(image_t)); + image = m_malloc(sizeof(image_t)); img_read_settings_t rs; imlib_read_geometry(&fp, image, path, &rs); file_close(&fp); @@ -313,7 +313,7 @@ float *py_helper_keyword_corner_array(size_t n_args, const mp_obj_t *args, size_ if (kw_arg) { mp_obj_t *arg_array; mp_obj_get_array_fixed_n(kw_arg->value, 4, &arg_array); - float *corners = xalloc(sizeof(float) * 8); + float *corners = m_malloc(sizeof(float) * 8); for (int i = 0; i < 4; i++) { mp_obj_t *arg_point; mp_obj_get_array_fixed_n(arg_array[i], 2, &arg_point); @@ -324,7 +324,7 @@ float *py_helper_keyword_corner_array(size_t n_args, const mp_obj_t *args, size_ } else if (n_args > arg_index) { mp_obj_t *arg_array; mp_obj_get_array_fixed_n(args[arg_index], 4, &arg_array); - float *corners = xalloc(sizeof(float) * 8); + float *corners = m_malloc(sizeof(float) * 8); for (int i = 0; i < 4; i++) { mp_obj_t *arg_point; mp_obj_get_array_fixed_n(arg_array[i], 2, &arg_point); diff --git a/modules/py_image.c b/modules/py_image.c index 6de5b9f4c..9f76e611b 100644 --- a/modules/py_image.c +++ b/modules/py_image.c @@ -39,7 +39,6 @@ #include "imlib.h" #include "array.h" #include "file_utils.h" -#include "xalloc.h" #include "fb_alloc.h" #include "framebuffer.h" #include "py_assert.h" @@ -1151,7 +1150,7 @@ static mp_obj_t py_image_to(pixformat_t pixfmt, mp_rom_obj_t default_color_palet py_helper_set_to_framebuffer(&dst_img); } else if (args[ARG_copy].u_bool) { // Create dynamic copy. - image_xalloc(&dst_img, size); + image_alloc(&dst_img, size); } else { // Convert in place. framebuffer_t *fb = framebuffer_get(0); @@ -1987,7 +1986,7 @@ static mp_obj_t py_image_binary(size_t n_args, const mp_obj_t *pos_args, mp_map_ out.pixfmt = args[ARG_to_bitmap].u_bool ? PIXFORMAT_BINARY : image->pixfmt; if (args[ARG_copy].u_bool) { - image_xalloc(&out, image_size(&out)); + image_alloc(&out, image_size(&out)); } else { out.data = image->data; } @@ -4498,10 +4497,10 @@ static mp_obj_t py_image_find_blobs(size_t n_args, const mp_obj_t *args, mp_map_ list_pop_front(&out, &lnk_data); objects_list->items[i] = py_blob_new(&lnk_data); if (lnk_data.x_hist_bins) { - xfree(lnk_data.x_hist_bins); + m_free(lnk_data.x_hist_bins); } if (lnk_data.y_hist_bins) { - xfree(lnk_data.y_hist_bins); + m_free(lnk_data.y_hist_bins); } } @@ -5090,7 +5089,7 @@ static mp_obj_t py_image_find_qrcodes(size_t n_args, const mp_obj_t *args, mp_ma o->eci = mp_obj_new_int(lnk_data.eci); objects_list->items[i] = o; - xfree(lnk_data.payload); + m_free(lnk_data.payload); } return objects_list; @@ -5523,7 +5522,7 @@ static mp_obj_t py_image_find_datamatrices(size_t n_args, const mp_obj_t *args, o->padding = mp_obj_new_int(lnk_data.padding); objects_list->items[i] = o; - xfree(lnk_data.payload); + m_free(lnk_data.payload); } return objects_list; @@ -5700,7 +5699,7 @@ static mp_obj_t py_image_find_barcodes(size_t n_args, const mp_obj_t *args, mp_m o->quality = mp_obj_new_int(lnk_data.quality); objects_list->items[i] = o; - xfree(lnk_data.payload); + m_free(lnk_data.payload); } return objects_list; @@ -6130,7 +6129,7 @@ mp_obj_t py_image_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw if (args[ARG_copy_to_fb].u_bool) { py_helper_set_to_framebuffer(&image); } else { - image_xalloc(&image, image_size(&image)); + image_alloc(&image, image_size(&image)); } imlib_load_image(&image, path); @@ -6171,7 +6170,7 @@ mp_obj_t py_image_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw mp_raise_ValueError(MP_ERROR_TEXT("Buffer is too small")); } } else { - image_xalloc(&image, image_size(&image)); + image_alloc(&image, image_size(&image)); } mp_float_t *farray = (mp_float_t *) array->array; @@ -6231,7 +6230,7 @@ mp_obj_t py_image_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw mp_raise_ValueError(MP_ERROR_TEXT("Buffer is too small")); } } else { - image_xalloc0(&image, image_size(&image)); + image_alloc0(&image, image_size(&image)); } } @@ -6869,7 +6868,7 @@ mp_obj_t py_image_load_descriptor(size_t n_args, const mp_obj_t *args, mp_map_t #if defined(IMLIB_ENABLE_FIND_KEYPOINTS) case DESC_ORB: { array_t *kpts = NULL; - array_alloc(&kpts, xfree); + array_alloc(&kpts, m_free); res = orb_load_descriptor(&fp, kpts); if (res == FR_OK) { diff --git a/modules/py_imageio.c b/modules/py_imageio.c index e4f9ac338..e2778ccf0 100644 --- a/modules/py_imageio.c +++ b/modules/py_imageio.c @@ -374,7 +374,7 @@ static mp_obj_t py_imageio_read(size_t n_args, const mp_obj_t *pos_args, mp_map_ if (args[ARG_copy_to_fb].u_bool) { py_helper_set_to_framebuffer(&image); } else { - image_xalloc(&image, size); + image_alloc(&image, size); } if (0) { diff --git a/modules/py_ml.c b/modules/py_ml.c index fa9c621ba..1e4b6bcf5 100644 --- a/modules/py_ml.c +++ b/modules/py_ml.c @@ -373,7 +373,7 @@ mp_obj_t py_ml_model_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } else { // Align size and memory and keep a reference to the GC block. size_t size = (model->size + IMLIB_ML_MODEL_ALIGN) & ~IMLIB_ML_MODEL_ALIGN; - model->_raw = xalloc(size + IMLIB_ML_MODEL_ALIGN); + model->_raw = m_malloc(size + IMLIB_ML_MODEL_ALIGN); model->data = (void *) (((uintptr_t) model->_raw + IMLIB_ML_MODEL_ALIGN) & ~IMLIB_ML_MODEL_ALIGN); } diff --git a/modules/py_tof.c b/modules/py_tof.c index 4ed5467e3..3a71087cc 100644 --- a/modules/py_tof.c +++ b/modules/py_tof.c @@ -548,7 +548,7 @@ mp_obj_t py_tof_snapshot(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_a if (args[ARG_copy_to_fb].u_bool) { py_helper_set_to_framebuffer(&dst_img); } else { - image_xalloc(&dst_img, image_size(&dst_img)); + image_alloc(&dst_img, image_size(&dst_img)); } float min = FLT_MAX; diff --git a/ports/rp2/omv_portconfig.cmake b/ports/rp2/omv_portconfig.cmake index 0043f2eba..e1f5994cd 100644 --- a/ports/rp2/omv_portconfig.cmake +++ b/ports/rp2/omv_portconfig.cmake @@ -119,7 +119,6 @@ target_sources(${MICROPY_TARGET} PRIVATE ${TOP_DIR}/common/file_utils.c ${TOP_DIR}/common/mp_utils.c ${TOP_DIR}/common/omv_csi.c - ${TOP_DIR}/common/xalloc.c ${TOP_DIR}/common/fb_alloc.c ${TOP_DIR}/common/umm_malloc.c ${TOP_DIR}/common/dma_alloc.c