Skip to content

Commit

Permalink
Use only one block of memory to cache frames.
Browse files Browse the repository at this point in the history
Maybe some other stuff too, but main point of this commit is: Instead of malloc(size_of_frame) for all frame caches, we use one big chunk instead, so memory is only allocated at the start of MLV object. I think this may reduce memory expansion as there will probably be less fragmentation.
  • Loading branch information
ilia3101 authored Jul 15, 2017
1 parent 8b2a138 commit 681e4f0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion platform/cocoa/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ objects = main.o video_mlv.o debayer.o amaze_demosaic.o raw_processing.o \
CC = gcc # Normal

# Flags for link and objects
mainflags = -O3 -Ofast
mainflags = -O3 -Ofast -m64
linkflags = $(mainflags) -mmacosx-version-min=10.6 -lm -pthread
cflags := $(mainflags) -c -mmacosx-version-min=10.6

Expand Down
11 changes: 9 additions & 2 deletions src/mlv/frame_caching.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ void setMlvRawCacheLimitMegaBytes(mlvObject_t * video, uint64_t megaByteLimit)
video->cache_limit_mb = megaByteLimit;
video->cache_limit_bytes = bytes_limit;

/* Resize cache block */
video->cache_memory_block = realloc(video->cache_memory_block, bytes_limit);

/* Protection against zero division, cuz that causes "Floating point exception: 8"...
* ...LOL there's not even a floating point in sight */
if (frame_size != 0 && video->is_active)
Expand Down Expand Up @@ -51,6 +54,9 @@ void setMlvRawCacheLimitFrames(mlvObject_t * video, uint64_t frameLimit)
video->cache_limit_mb = mbyte_limit;
video->cache_limit_frames = frameLimit;

/* Resize cache block */
video->cache_memory_block = realloc(video->cache_memory_block, bytes_limit);

/* Begin updating cached frames */
if (!video->is_caching)
{
Expand All @@ -69,7 +75,7 @@ void cache_mlv_frames(mlvObject_t * video)
int height = getMlvHeight(video);
int threads = getMlvCpuCores(video) / 2 + 1;
int cache_frames = MIN((int)video->cache_limit_frames, (int)video->frames);
size_t frame_size_rgb = width * height * sizeof(uint16_t) * 3;
int frame_size_rgb = width * height * 3;

float * raw_frame = malloc( getMlvWidth(video) * getMlvHeight(video) * sizeof(float) );

Expand All @@ -83,7 +89,8 @@ void cache_mlv_frames(mlvObject_t * video)
/* Only debayer if frame is not already cached and has not been requested to stop */
if (!video->cached_frames[frame_index] && !video->stop_caching)
{
video->rgb_raw_frames[frame_index] = (uint16_t *)malloc( frame_size_rgb );
/* Use memory within our block */
video->rgb_raw_frames[frame_index] = video->cache_memory_block + (frame_size_rgb * frame_index);

/* debayer_type 1, we want to cache AMaZE frames */
get_mlv_raw_frame_debayered(video, frame_index, raw_frame, video->rgb_raw_frames[frame_index], 1);
Expand Down
4 changes: 4 additions & 0 deletions src/mlv/mlv_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ typedef struct {
* for each frame: 0(false) = frame is cached, 1 or more(true) = frame is cached */
uint16_t ** rgb_raw_frames; /* Pointers to 16/48bpp debayered RGB frames */

/* Massive block of memory for all frames that will be cached, pointers in rgb_raw_frames will point within here,
* using one big block block to try and avoid fragmentation (I feel that may be one of the causes of growth) */
uint16_t * cache_memory_block;

/* How many cores, will not neccesarily determine number of threads made in any case, but helps */
int cpu_cores; /* Default 4 */

Expand Down
19 changes: 7 additions & 12 deletions src/mlv/video_mlv.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,19 @@ mlvObject_t * initMlvObject()
/* Just 1 element for now */
video->frame_offsets = (uint32_t *)malloc( sizeof(uint32_t) );

/* Cache things, only one element for now as it is empty */
video->rgb_raw_frames = (uint16_t **)malloc( sizeof(uint16_t *) );
video->cached_frames = (uint8_t *)malloc( sizeof(uint8_t) );
/* All frames in one block of memory for least mallocing during usage */
video->cache_memory_block = (uint16_t *)malloc( sizeof(uint16_t) );

/* Set cache limit to allow ~1 second of 1080p and be safe for low ram PCs */
setMlvRawCacheLimitMegaBytes(video, 290);
setMlvCacheStartFrame(video, 0); /* Just in case */

/* Seems about right */
setMlvCpuCores(video, 4);

/* Cache things, only one element for now as it is empty */
video->rgb_raw_frames = (uint16_t **)malloc( sizeof(uint16_t *) );
video->cached_frames = (uint8_t *)malloc( sizeof(uint8_t) );

/* Retun pointer */
return video;
}
Expand All @@ -243,17 +245,10 @@ void freeMlvObject(mlvObject_t * video)
video->stop_caching = 1;
while (video->is_caching) usleep(100);

for (int f = 0; f < video->frames; ++f)
{
/* If frame is cached we can free it */
if (video->cached_frames[f])
{
free(video->rgb_raw_frames[f]);
}
}
/* Now free these */
free(video->cached_frames);
free(video->rgb_raw_frames);
free(video->cache_memory_block);

/* Main 1 */
free(video);
Expand Down

0 comments on commit 681e4f0

Please sign in to comment.