GUACAMOLE-1867: Migrate guacenc utility to new memory management functions.

This commit is contained in:
Mike Jumper 2023-10-20 14:09:56 -07:00
parent 944370bdb6
commit 60ae3520ee
9 changed files with 44 additions and 30 deletions

View File

@ -21,12 +21,13 @@
#include "buffer.h"
#include <cairo/cairo.h>
#include <guacamole/mem.h>
#include <assert.h>
#include <stdlib.h>
guacenc_buffer* guacenc_buffer_alloc() {
return calloc(1, sizeof(guacenc_buffer));
return guac_mem_zalloc(sizeof(guacenc_buffer));
}
/**
@ -52,7 +53,7 @@ static void guacenc_buffer_free_image(guacenc_buffer* buffer) {
}
/* Free image data (previously wrapped by Cairo surface */
free(buffer->image);
guac_mem_free(buffer->image);
buffer->image = NULL;
}
@ -65,7 +66,7 @@ void guacenc_buffer_free(guacenc_buffer* buffer) {
/* Free buffer and underlying image */
guacenc_buffer_free_image(buffer);
free(buffer);
guac_mem_free(buffer);
}
@ -86,7 +87,7 @@ int guacenc_buffer_resize(guacenc_buffer* buffer, int width, int height) {
/* Allocate data for new image */
int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
unsigned char* image = calloc(1, stride*height);
unsigned char* image = guac_mem_zalloc(stride, height);
/* Wrap data in surface */
cairo_surface_t* surface = cairo_image_surface_create_for_data(image,

View File

@ -21,19 +21,21 @@
#include "buffer.h"
#include "cursor.h"
#include <guacamole/mem.h>
#include <stdlib.h>
guacenc_cursor* guacenc_cursor_alloc() {
/* Allocate new cursor */
guacenc_cursor* cursor = (guacenc_cursor*) malloc(sizeof(guacenc_cursor));
guacenc_cursor* cursor = (guacenc_cursor*) guac_mem_alloc(sizeof(guacenc_cursor));
if (cursor == NULL)
return NULL;
/* Allocate associated buffer (image) */
cursor->buffer = guacenc_buffer_alloc();
if (cursor->buffer == NULL) {
free(cursor);
guac_mem_free(cursor);
return NULL;
}
@ -53,7 +55,7 @@ void guacenc_cursor_free(guacenc_cursor* cursor) {
/* Free underlying buffer */
guacenc_buffer_free(cursor->buffer);
free(cursor);
guac_mem_free(cursor);
}

View File

@ -23,6 +23,7 @@
#include "video.h"
#include <cairo/cairo.h>
#include <guacamole/mem.h>
#include <stdlib.h>
@ -93,7 +94,7 @@ guacenc_display* guacenc_display_alloc(const char* path, const char* codec,
/* Allocate display */
guacenc_display* display =
(guacenc_display*) calloc(1, sizeof(guacenc_display));
(guacenc_display*) guac_mem_zalloc(sizeof(guacenc_display));
/* Associate display with video output */
display->output = video;
@ -131,7 +132,7 @@ int guacenc_display_free(guacenc_display* display) {
/* Free cursor */
guacenc_cursor_free(display->cursor);
free(display);
guac_mem_free(display);
return retval;
}

View File

@ -26,6 +26,7 @@
#include <libavutil/common.h>
#include <libavutil/imgutils.h>
#include <guacamole/client.h>
#include <guacamole/mem.h>
#include <stdio.h>
#include <stdint.h>
@ -106,10 +107,11 @@ int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) {
AVCodecContext* context = video->context;
/* Calculate appropriate buffer size */
int length = FF_MIN_BUFFER_SIZE + 12 * context->width * context->height;
size_t length = guac_mem_ckd_add_or_die(FF_MIN_BUFFER_SIZE,
guac_mem_ckd_mul_or_die(12, context->width, context->height));
/* Allocate space for output */
uint8_t* data = malloc(length);
uint8_t* data = guac_mem_alloc(length);
if (data == NULL)
return -1;
@ -118,19 +120,19 @@ int guacenc_avcodec_encode_video(guacenc_video* video, AVFrame* frame) {
if (used < 0) {
guacenc_log(GUAC_LOG_WARNING, "Error encoding frame #%" PRId64,
video->next_pts);
free(data);
guac_mem_free(data);
return -1;
}
/* Report if no data needs to be written */
if (used == 0) {
free(data);
guac_mem_free(data);
return 0;
}
/* Write data, logging any errors */
guacenc_write_packet(video, data, used);
free(data);
guac_mem_free(data);
return 1;
#else

View File

@ -29,6 +29,7 @@
#endif
#include <cairo/cairo.h>
#include <guacamole/mem.h>
#include <stdlib.h>
#include <string.h>
@ -67,7 +68,7 @@ guacenc_image_stream* guacenc_image_stream_alloc(int mask, int index,
const char* mimetype, int x, int y) {
/* Allocate stream */
guacenc_image_stream* stream = malloc(sizeof(guacenc_image_stream));
guacenc_image_stream* stream = guac_mem_alloc(sizeof(guacenc_image_stream));
if (stream == NULL)
return NULL;
@ -83,7 +84,7 @@ guacenc_image_stream* guacenc_image_stream_alloc(int mask, int index,
/* Allocate initial buffer */
stream->length = 0;
stream->max_length = GUACENC_IMAGE_STREAM_INITIAL_LENGTH;
stream->buffer = (unsigned char*) malloc(stream->max_length);
stream->buffer = (unsigned char*) guac_mem_alloc(stream->max_length);
return stream;
@ -96,11 +97,12 @@ int guacenc_image_stream_receive(guacenc_image_stream* stream,
if (stream->max_length - stream->length < length) {
/* Calculate a reasonable new max length guaranteed to fit buffer */
int new_max_length = stream->max_length * 2 + length;
size_t new_max_length = guac_mem_ckd_add_or_die(
guac_mem_ckd_mul_or_die(stream->max_length, 2), length);
/* Attempt to resize buffer */
unsigned char* new_buffer =
(unsigned char*) realloc(stream->buffer, new_max_length);
(unsigned char*) guac_mem_realloc(stream->buffer, new_max_length);
if (new_buffer == NULL)
return 1;
@ -158,10 +160,10 @@ int guacenc_image_stream_free(guacenc_image_stream* stream) {
return 0;
/* Free image buffer */
free(stream->buffer);
guac_mem_free(stream->buffer);
/* Free actual stream */
free(stream);
guac_mem_free(stream);
return 0;
}

View File

@ -25,6 +25,8 @@
#include <cairo/cairo.h>
#include <stddef.h>
/**
* The initial number of bytes to allocate for the image data buffer. If this
* buffer is not sufficiently large, it will be dynamically reallocated as it
@ -87,13 +89,13 @@ typedef struct guacenc_image_stream {
/**
* The number of bytes currently stored in the buffer.
*/
int length;
size_t length;
/**
* The maximum number of bytes that can be stored in the current buffer
* before it must be reallocated.
*/
int max_length;
size_t max_length;
/**
* The decoder to use when decoding the raw data received along this

View File

@ -25,6 +25,7 @@
#include <unistd.h>
#include <cairo/cairo.h>
#include <guacamole/mem.h>
#include <jpeglib.h>
#include <stdlib.h>
@ -109,7 +110,7 @@ cairo_surface_t* guacenc_jpeg_decoder(unsigned char* data, int length) {
int height = cinfo.output_height;
/* Allocate sufficient buffer space for one JPEG scanline */
unsigned char* jpeg_scanline = malloc(width * 3);
unsigned char* jpeg_scanline = guac_mem_alloc(width, 3);
/* Create blank Cairo surface (no transparency in JPEG) */
cairo_surface_t* surface = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
@ -135,7 +136,7 @@ cairo_surface_t* guacenc_jpeg_decoder(unsigned char* data, int length) {
}
/* Scanline buffer is no longer needed */
free(jpeg_scanline);
guac_mem_free(jpeg_scanline);
/* End decompression */
jpeg_finish_decompress(&cinfo);

View File

@ -21,19 +21,21 @@
#include "buffer.h"
#include "layer.h"
#include <guacamole/mem.h>
#include <stdlib.h>
guacenc_layer* guacenc_layer_alloc() {
/* Allocate new layer */
guacenc_layer* layer = (guacenc_layer*) calloc(1, sizeof(guacenc_layer));
guacenc_layer* layer = (guacenc_layer*) guac_mem_zalloc(sizeof(guacenc_layer));
if (layer == NULL)
return NULL;
/* Allocate associated buffer (width, height, and image storage) */
layer->buffer = guacenc_buffer_alloc();
if (layer->buffer == NULL) {
free(layer);
guac_mem_free(layer);
return NULL;
}
@ -41,7 +43,7 @@ guacenc_layer* guacenc_layer_alloc() {
layer->frame = guacenc_buffer_alloc();
if (layer->frame== NULL) {
guacenc_buffer_free(layer->buffer);
free(layer);
guac_mem_free(layer);
return NULL;
}
@ -67,7 +69,7 @@ void guacenc_layer_free(guacenc_layer* layer) {
/* Free underlying buffer */
guacenc_buffer_free(layer->buffer);
free(layer);
guac_mem_free(layer);
}

View File

@ -32,6 +32,7 @@
#include <libavutil/imgutils.h>
#include <libswscale/swscale.h>
#include <guacamole/client.h>
#include <guacamole/mem.h>
#include <guacamole/timestamp.h>
#include <sys/types.h>
@ -138,7 +139,7 @@ guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name,
}
/* Allocate video structure */
guacenc_video* video = malloc(sizeof(guacenc_video));
guacenc_video* video = guac_mem_alloc(sizeof(guacenc_video));
if (video == NULL)
goto fail_alloc_video;
@ -503,7 +504,7 @@ int guacenc_video_free(guacenc_video* video) {
avcodec_free_context(&(video->context));
}
free(video);
guac_mem_free(video);
return 0;
}