Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions apriltag_quad_thresh.c
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,16 @@ static inline void ptsort(struct pt *pts, int sz)
#undef MAYBE_SWAP

// a merge sort with temp storage.

struct pt *tmp = malloc(sizeof(struct pt) * sz);
// Use stack allocation for small arrays to avoid malloc overhead
#define STACK_BUFFER_SIZE 256
struct pt stack_buffer[STACK_BUFFER_SIZE];
struct pt *tmp;
const bool use_heap = sz > STACK_BUFFER_SIZE;
if (use_heap) {
tmp = malloc(sizeof(struct pt) * sz);
} else {
tmp = stack_buffer;
}

memcpy(tmp, pts, sizeof(struct pt) * sz);

Expand Down Expand Up @@ -749,7 +757,9 @@ static inline void ptsort(struct pt *pts, int sz)
if (bpos < bsz)
memcpy(&pts[outpos], &bs[bpos], (bsz-bpos)*sizeof(struct pt));

free(tmp);
if (use_heap) {
free(tmp);
}

#undef MERGE
}
Expand Down
Loading