Replace malloc calls with xalloc

This commit is contained in:
iabdalkader 2014-02-23 23:26:04 +02:00
parent fa42d9c565
commit d00ee2d083
12 changed files with 170 additions and 76 deletions

Binary file not shown.

View File

@ -4,6 +4,7 @@
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "xalloc.h"
#include "array.h" #include "array.h"
#define ARRAY_INIT_SIZE 10 #define ARRAY_INIT_SIZE 10
struct array { struct array {
@ -16,11 +17,11 @@ struct array {
void array_alloc(struct array **a, array_dtor dtor) void array_alloc(struct array **a, array_dtor dtor)
{ {
struct array *array; struct array *array;
array = malloc(sizeof(struct array)); array = xalloc(sizeof(struct array));
array->index = 0; array->index = 0;
array->length = ARRAY_INIT_SIZE; array->length = ARRAY_INIT_SIZE;
array->dtor = dtor; array->dtor = dtor;
array->data = malloc(array->length*sizeof(void*)); array->data = xalloc(array->length*sizeof(void*));
*a = array; *a = array;
} }
@ -30,8 +31,8 @@ void array_free(struct array *array)
for (i=0; i<array->index; i++){ for (i=0; i<array->index; i++){
array->dtor(array->data[i]); array->dtor(array->data[i]);
} }
free(array->data); xfree(array->data);
free(array); xfree(array);
} }
int array_length(struct array *array) int array_length(struct array *array)
@ -43,7 +44,7 @@ void array_push_back(struct array *array, void *element)
{ {
if (array->index == array->length-1) { if (array->index == array->length-1) {
array->length += ARRAY_INIT_SIZE; array->length += ARRAY_INIT_SIZE;
array->data = realloc(array->data, array->length * sizeof(void*)); array->data = xrealloc(array->data, array->length * sizeof(void*));
} }
array->data[array->index++] = element; array->data[array->index++] = element;
} }

View File

@ -6,6 +6,7 @@
#include "array.h" #include "array.h"
#include "imlib.h" #include "imlib.h"
#include "ff.h" #include "ff.h"
#include "xalloc.h"
#define MIN(a,b) \ #define MIN(a,b) \
({ __typeof__ (a) _a = (a); \ ({ __typeof__ (a) _a = (a); \
@ -145,7 +146,7 @@ void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size)
int w = src->w; int w = src->w;
int h = src->h; int h = src->h;
/* TODO */ /* TODO */
uint8_t *dst = calloc(w*h, 1); uint8_t *dst = xcalloc(w*h, 1);
for (y=0; y<h-k_size; y++) { for (y=0; y<h-k_size; y++) {
for (x=0; x<w-k_size; x++) { for (x=0; x<w-k_size; x++) {
@ -163,7 +164,7 @@ void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size)
} }
} }
memcpy(src->pixels, dst, w*h); memcpy(src->pixels, dst, w*h);
free(dst); xfree(dst);
} }
int imlib_image_mean(struct image *src) int imlib_image_mean(struct image *src)
@ -208,27 +209,6 @@ void imlib_blit(struct image *dst_img, struct image *src_img, int x_off, int y_o
} }
} }
void imlib_integral_image(struct image *src, struct integral_image *sum)
{
int x, y, s,t;
unsigned char *data = src->pixels;
typeof(*sum->data) *sumData = sum->data;
for (y=0; y<src->h; y++) {
s = 0;
/* loop over the number of columns */
for (x=0; x<src->w; x++) {
/* sum of the current row (integer)*/
s += data[y*src->w+x];
t = s;
if (y != 0) {
t += sumData[(y-1)*src->w+x];
}
sumData[y*src->w+x]=t;
}
}
}
void imlib_scale_image(struct image *src, struct image *dst) void imlib_scale_image(struct image *src, struct image *dst)
{ {
int x, y, i, j; int x, y, i, j;
@ -279,10 +259,10 @@ void imlib_draw_rectangle(struct image *image, struct rectangle *r)
for (i=0; i<h; i++) { for (i=0; i<h; i++) {
image->pixels[(y+i)*col + x] = c; image->pixels[(y+i)*col + x] = c;
image->pixels[(y+i)*col + x + w-2] = c; image->pixels[(y+i)*col + x + w] = c;
if (image->bpp>1) { if (image->bpp>1) {
image->pixels[(y+i)*col + x+1] = c; image->pixels[(y+i)*col + x+1] = c;
image->pixels[(y+i)*col + x + w-1] = c; image->pixels[(y+i)*col + x + w+1] = c;
} }
} }
} }
@ -309,6 +289,29 @@ void imlib_histeq(struct image *src)
} }
} }
int imlib_image_sumsq(struct image *image, int u, int v, int w, int h)
{
vec_t v0;
vec_t v1;
int x,y;
int sumsq=0;
int offset=0;
for (y=v; y<h+v; y++) {
for (x=u; x<w+u; x+=2) {
offset = y*image->w+x;
v0.s0 = image->data[offset+0];
v0.s1 = image->data[offset+1];
v1.s0 = image->data[offset+0];
v1.s1 = image->data[offset+1];
sumsq = __SMLAD(v0.i, v1.i, sumsq);
}
}
return sumsq;
}
/* Viola-Jones face detector implementation /* Viola-Jones face detector implementation
* Original Author: Francesco Comaschi (f.comaschi@tue.nl) * Original Author: Francesco Comaschi (f.comaschi@tue.nl)
*/ */
@ -425,7 +428,7 @@ static void ScaleImageInvoker(struct cascade *cascade, float factor, int sum_row
/* If a face is detected, record the coordinates of the filter window */ /* If a face is detected, record the coordinates of the filter window */
if (result > 0) { if (result > 0) {
struct rectangle *r = malloc(sizeof(struct rectangle)); struct rectangle *r = xalloc(sizeof(struct rectangle));
r->x = roundf(x*factor); r->x = roundf(x*factor);
r->y = roundf(y*factor); r->y = roundf(y*factor);
r->w = win_size.w; r->w = win_size.w;
@ -439,7 +442,7 @@ static void ScaleImageInvoker(struct cascade *cascade, float factor, int sum_row
struct rectangle *rectangle_clone(struct rectangle *rect) struct rectangle *rectangle_clone(struct rectangle *rect)
{ {
struct rectangle *rectangle; struct rectangle *rectangle;
rectangle = malloc(sizeof(struct rectangle)); rectangle = xalloc(sizeof(struct rectangle));
memcpy(rectangle, rect, sizeof(struct rectangle)); memcpy(rectangle, rect, sizeof(struct rectangle));
return rectangle; return rectangle;
} }
@ -484,8 +487,8 @@ struct array *imlib_merge_detections(struct array *rectangles)
struct array *overlap; struct array *overlap;
struct rectangle *rect1, *rect2; struct rectangle *rect1, *rect2;
array_alloc(&objects, free); array_alloc(&objects, xfree);
array_alloc(&overlap, free); array_alloc(&overlap, xfree);
/* merge overlaping detections */ /* merge overlaping detections */
while (array_length(rectangles)) { while (array_length(rectangles)) {
@ -538,11 +541,11 @@ struct array *imlib_detect_objects(struct image *image, struct cascade *cascade)
/* allocate buffer for integral image */ /* allocate buffer for integral image */
sum.w = image->w; sum.w = image->w;
sum.h = image->h; sum.h = image->h;
//sum.data = malloc(image->w *image->h*sizeof(*sum.data)); //sum.data = xalloc(image->w *image->h*sizeof(*sum.data));
sum.data = (uint32_t*) (image->pixels+(image->w * image->h * 2)); sum.data = (uint32_t*) (image->pixels+(image->w * image->h * 2));
/* allocate the detections array */ /* allocate the detections array */
array_alloc(&objects, free); array_alloc(&objects, xfree);
/* set cascade image pointer */ /* set cascade image pointer */
cascade->img = &img; cascade->img = &img;
@ -581,7 +584,7 @@ struct array *imlib_detect_objects(struct image *image, struct cascade *cascade)
ScaleImageInvoker(cascade, factor, sum.h, sum.w, objects); ScaleImageInvoker(cascade, factor, sum.h, sum.w, objects);
} }
//free(sum.data); //xfree(sum.data);
objects = imlib_merge_detections(objects); objects = imlib_merge_detections(objects);
return objects; return objects;
@ -612,8 +615,8 @@ int imlib_load_cascade(struct cascade* cascade, const char *path)
goto error; goto error;
} }
cascade->stages_array = malloc (sizeof(*cascade->stages_array) * cascade->n_stages); cascade->stages_array = xalloc (sizeof(*cascade->stages_array) * cascade->n_stages);
cascade->stages_thresh_array = malloc (sizeof(*cascade->stages_thresh_array) * cascade->n_stages); cascade->stages_thresh_array = xalloc (sizeof(*cascade->stages_thresh_array) * cascade->n_stages);
if (cascade->stages_array == NULL || if (cascade->stages_array == NULL ||
cascade->stages_thresh_array == NULL) { cascade->stages_thresh_array == NULL) {
@ -633,10 +636,10 @@ int imlib_load_cascade(struct cascade* cascade, const char *path)
} }
/* alloc features thresh array, alpha1, alpha 2,rects weights and rects*/ /* alloc features thresh array, alpha1, alpha 2,rects weights and rects*/
cascade->tree_thresh_array = malloc (sizeof(*cascade->tree_thresh_array) * cascade->n_features); cascade->tree_thresh_array = xalloc (sizeof(*cascade->tree_thresh_array) * cascade->n_features);
cascade->alpha1_array = malloc (sizeof(*cascade->alpha1_array) * cascade->n_features); cascade->alpha1_array = xalloc (sizeof(*cascade->alpha1_array) * cascade->n_features);
cascade->alpha2_array = malloc (sizeof(*cascade->alpha2_array) * cascade->n_features); cascade->alpha2_array = xalloc (sizeof(*cascade->alpha2_array) * cascade->n_features);
cascade->num_rectangles_array = malloc (sizeof(*cascade->num_rectangles_array) * cascade->n_features); cascade->num_rectangles_array = xalloc (sizeof(*cascade->num_rectangles_array) * cascade->n_features);
if (cascade->tree_thresh_array == NULL || if (cascade->tree_thresh_array == NULL ||
cascade->alpha1_array == NULL || cascade->alpha1_array == NULL ||
@ -681,8 +684,8 @@ int imlib_load_cascade(struct cascade* cascade, const char *path)
cascade->n_rectangles += cascade->num_rectangles_array[i]; cascade->n_rectangles += cascade->num_rectangles_array[i];
} }
cascade->weights_array = malloc (sizeof(*cascade->weights_array) * cascade->n_rectangles); cascade->weights_array = xalloc (sizeof(*cascade->weights_array) * cascade->n_rectangles);
cascade->rectangles_array = malloc (sizeof(*cascade->rectangles_array) * cascade->n_rectangles * 4); cascade->rectangles_array = xalloc (sizeof(*cascade->rectangles_array) * cascade->n_rectangles * 4);
if (cascade->weights_array == NULL || if (cascade->weights_array == NULL ||
cascade->rectangles_array == NULL) { cascade->rectangles_array == NULL) {
@ -707,19 +710,4 @@ error:
return res; return res;
} }
uint32_t imlib_integral_lookup(struct integral_image *src, int x, int y, int w, int h)
{
#define PIXEL_AT(x,y)\
(src->data[src->w*(y-1)+(x-1)])
if (x==0 && y==0) {
return PIXEL_AT(w,h);
} else if (y==0) {
return PIXEL_AT(w+x, h+y) - PIXEL_AT(x, h+y);
} else if (x==0) {
return PIXEL_AT(w+x, h+y) - PIXEL_AT(w+x, y);
} else {
return PIXEL_AT(w+x, h+y) + PIXEL_AT(x, y) - PIXEL_AT(w+x, y) - PIXEL_AT(x, h+y);
}
#undef PIXEL_AT
}

View File

@ -102,7 +102,6 @@ void imlib_grayscale_to_rgb565(struct image *image);
void imlib_detect_color(struct image *image, struct color *color, struct rectangle *rectangle, int threshold); void imlib_detect_color(struct image *image, struct color *color, struct rectangle *rectangle, int threshold);
void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size); void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size);
void imlib_scale_image(struct image *src, struct image *dst); void imlib_scale_image(struct image *src, struct image *dst);
void imlib_integral_image(struct image *src, struct integral_image *sum);
void imlib_draw_rectangle(struct image *image, struct rectangle *r); void imlib_draw_rectangle(struct image *image, struct rectangle *r);
void imlib_histeq(struct image *src); void imlib_histeq(struct image *src);
struct array *imlib_detect_objects(struct image *image, struct cascade* cascade); struct array *imlib_detect_objects(struct image *image, struct cascade* cascade);
@ -110,8 +109,10 @@ int imlib_load_cascade(struct cascade* cascade, const char *path);
float imlib_template_match(struct image *image, struct image *template, struct rectangle *r); float imlib_template_match(struct image *image, struct image *template, struct rectangle *r);
int imlib_save_template(struct image *image, const char *path); int imlib_save_template(struct image *image, const char *path);
int imlib_load_template(struct image *image, const char *path); int imlib_load_template(struct image *image, const char *path);
uint32_t imlib_integral_lookup(struct integral_image *src, int x, int y, int w, int h);
int imlib_image_mean(struct image *src); int imlib_image_mean(struct image *src);
void imlib_subimage(struct image *src_img, struct image *dst_img, int x_off, int y_off); void imlib_subimage(struct image *src_img, struct image *dst_img, int x_off, int y_off);
void imlib_blit(struct image *dst_img, struct image *src_img, int x_off, int y_off); void imlib_blit(struct image *dst_img, struct image *src_img, int x_off, int y_off);
void imlib_integral_image(struct image *src, struct integral_image *sum);
void imlib_integral_image_sq(struct image *src, struct integral_image *sum);
uint32_t imlib_integral_lookup(struct integral_image *src, int x, int y, int w, int h);
#endif //__IMLIB_H__ #endif //__IMLIB_H__

66
src/integral.c Normal file
View File

@ -0,0 +1,66 @@
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <arm_math.h>
#include "imlib.h"
void imlib_integral_image(struct image *src, struct integral_image *sum)
{
int x, y, s,t;
unsigned char *data = src->pixels;
typeof(*sum->data) *sumData = sum->data;
for (y=0; y<src->h; y++) {
s = 0;
/* loop over the number of columns */
for (x=0; x<src->w; x++) {
/* sum of the current row (integer)*/
s += data[y*src->w+x];
t = s;
if (y != 0) {
t += sumData[(y-1)*src->w+x];
}
sumData[y*src->w+x]=t;
}
}
}
void imlib_integral_image_sq(struct image *src, struct integral_image *sum)
{
int x, y, s,t;
typeof(*src->data) *data = src->data;
typeof(*sum->data) *sumData = sum->data;
for (y=0; y<src->h; y++) {
s = 0;
/* loop over the number of columns */
for (x=0; x<src->w; x++) {
/* sum of the current row (integer)*/
s += (data[y*src->w+x])*(data[y*src->w+x]);
t = s;
if (y != 0) {
t += sumData[(y-1)*src->w+x];
}
sumData[y*src->w+x]=t;
}
}
}
uint32_t imlib_integral_lookup(struct integral_image *src, int x, int y, int w, int h)
{
#define PIXEL_AT(x,y)\
(src->data[src->w*(y-1)+(x-1)])
if (x==0 && y==0) {
return PIXEL_AT(w,h);
} else if (y==0) {
return PIXEL_AT(w+x, h+y) - PIXEL_AT(x, h+y);
} else if (x==0) {
return PIXEL_AT(w+x, h+y) - PIXEL_AT(w+x, y);
} else {
return PIXEL_AT(w+x, h+y) + PIXEL_AT(x, y) - PIXEL_AT(w+x, y) - PIXEL_AT(x, h+y);
}
#undef PIXEL_AT
}

View File

@ -28,7 +28,7 @@ static const mp_method_t py_image_methods[] = {
{ NULL, NULL }, { NULL, NULL },
}; };
const mp_obj_type_t py_image_type = { static const mp_obj_type_t py_image_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_Image, .name = MP_QSTR_Image,
.print = py_image_print, .print = py_image_print,

View File

@ -1,4 +1,5 @@
#include <libmp.h> #include <libmp.h>
#include "xalloc.h"
#include "imlib.h" #include "imlib.h"
#include "array.h" #include "array.h"
#include "sensor.h" #include "sensor.h"
@ -29,7 +30,7 @@ static void py_cascade_print(void (*print)(void *env, const char *fmt, ...), voi
static const mp_obj_type_t py_cascade_type = { static const mp_obj_type_t py_cascade_type = {
{ &mp_type_type }, { &mp_type_type },
"Cascade", .name = MP_QSTR_Cascade,
.print = py_cascade_print, .print = py_cascade_print,
}; };
@ -201,12 +202,12 @@ mp_obj_t py_imlib_save_template(mp_obj_t image_obj, mp_obj_t rectangle_obj, mp_o
image = py_image_cobj(image_obj); image = py_image_cobj(image_obj);
t.w = r.w; t.w = r.w;
t.h = r.h; t.h = r.h;
t.data = malloc(sizeof(*t.data)*t.w*t.h); t.data = xalloc(sizeof(*t.data)*t.w*t.h);
imlib_subimage(image, &t, r.x, r.y); imlib_subimage(image, &t, r.x, r.y);
int res = imlib_save_template(&t, path); int res = imlib_save_template(&t, path);
free(t.data); xfree(t.data);
if (res != FR_OK) { if (res != FR_OK) {
nlr_jump(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res))); nlr_jump(mp_obj_new_exception_msg(&mp_type_OSError, ffs_strerror(res)));

View File

@ -1,8 +1,9 @@
#include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <stm32f4xx.h> #include <stm32f4xx.h>
#include <stm32f4xx_misc.h> #include <stm32f4xx_misc.h>
#include <stdlib.h> #include "xalloc.h"
#include "systick.h" #include "systick.h"
#include "array.h" #include "array.h"
static volatile uint32_t sys_ticks; static volatile uint32_t sys_ticks;
@ -33,7 +34,7 @@ void SysTick_Handler(void)
int systick_init() int systick_init()
{ {
/* Allocate task_list array */ /* Allocate task_list array */
//array_alloc(&task_list, free); //array_alloc(&task_list, xfree);
/* Configure systick to interrupt every 1ms */ /* Configure systick to interrupt every 1ms */
if (SysTick_Config(SystemCoreClock / 1000)) { if (SysTick_Config(SystemCoreClock / 1000)) {
@ -61,7 +62,7 @@ void systick_sched_task(task_cb cb, uint32_t period)
#if 0 #if 0
struct systick_task *task; struct systick_task *task;
task = malloc(sizeof(struct systick_task)); task = xalloc(sizeof(struct systick_task));
task->cb = cb; task->cb = cb;
task->period = period; task->period = period;
array_push_back(task_list, task); array_push_back(task_list, task);

View File

@ -1,6 +1,6 @@
#include "ff.h"
#include "imlib.h"
#include <libmp.h> #include <libmp.h>
#include "xalloc.h"
#include "imlib.h"
#include <math.h> #include <math.h>
#include <arm_math.h> #include <arm_math.h>
@ -64,7 +64,7 @@ int imlib_load_template(struct image *image, const char *path)
} }
printf("temp:%d %d \n", image->w, image->h); printf("temp:%d %d \n", image->w, image->h);
image->data = malloc(sizeof(*image->data)*image->w*image->h); image->data = xalloc(sizeof(*image->data)*image->w*image->h);
if (image->data == NULL) { if (image->data == NULL) {
goto error; goto error;
} }
@ -95,7 +95,7 @@ float imlib_template_match(struct image *f, struct image *t_orig, struct rectan
t->w = t_orig->w; t->w = t_orig->w;
t->h = t_orig->h; t->h = t_orig->h;
t->bpp = t_orig->bpp; t->bpp = t_orig->bpp;
t->data = malloc(sizeof(*t->data)* t_orig->w * t_orig->h); t->data = xalloc(sizeof(*t->data)* t_orig->w * t_orig->h);
/* allocate buffer for integral image */ /* allocate buffer for integral image */
f_imgs->w = f->w; f_imgs->w = f->w;
@ -155,6 +155,6 @@ float imlib_template_match(struct image *f, struct image *t_orig, struct rectan
} }
} }
free(t->data); xfree(t->data);
return corr; return corr;
} }

View File

@ -81,6 +81,7 @@ void usbdbg_control(uint8_t request, int length)
mp_obj_exception_clear_traceback(mp_const_ide_interrupt); mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
pendsv_nlr_jump(mp_const_ide_interrupt); pendsv_nlr_jump(mp_const_ide_interrupt);
} }
cmd = USBDBG_NONE;
break; break;
default: /* error */ default: /* error */

27
src/xalloc.c Normal file
View File

@ -0,0 +1,27 @@
#include <libmp.h>
#include "xalloc.h"
void *xcalloc(size_t nmemb, size_t size)
{
void *mem = gc_alloc(nmemb*size);
if (mem) {
bzero(mem, nmemb*size);
}
return mem;
}
void *xalloc(size_t size)
{
return gc_alloc(size);
}
void xfree(void *ptr)
{
gc_free(ptr);
}
void *xrealloc(void *ptr, size_t size)
{
return gc_realloc(ptr, size);
}

8
src/xalloc.h Normal file
View File

@ -0,0 +1,8 @@
#ifndef __XALLOC_H__
#define __XALLOC_H__
#include <stdint.h>
void *xcalloc(size_t nmemb, size_t size);
void *xalloc(size_t size);
void xfree(void *ptr);
void *xrealloc(void *ptr, size_t size);
#endif /* __XALLOC_H__ */