From b9a50bad00d00b8afc747a37281d7f5422da6198 Mon Sep 17 00:00:00 2001 From: Kentaro Fukuchi Date: Wed, 10 Sep 2014 00:39:40 +0900 Subject: [PATCH] FrameFiller allocation has been moved from heap to stack. --- ChangeLog | 2 ++ qrencode.c | 60 ++++++++++++++---------------------------------------- 2 files changed, 17 insertions(+), 45 deletions(-) diff --git a/ChangeLog b/ChangeLog index 46e0deb2..f57bd656 100644 --- a/ChangeLog +++ b/ChangeLog @@ -14,6 +14,8 @@ tests/test_mmask.c, tests/test_monkey.c, tests/test_mqrspec.c, tests/test_qrencode.c, tests/test_qrspec.c: - Removed cache clearing calls. + * qrencode.c: + - FrameFiller now allocated in stack, not heap. 2014.09.08 Kentaro FUKUCHI * qrenc.c: diff --git a/qrencode.c b/qrencode.c index e61340d2..02de8188 100644 --- a/qrencode.c +++ b/qrencode.c @@ -284,12 +284,8 @@ typedef struct { int mqr; } FrameFiller; -static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr) +static void FrameFiller_set(FrameFiller *filler, int width, unsigned char *frame, int mqr) { - FrameFiller *filler; - - filler = (FrameFiller *)malloc(sizeof(FrameFiller)); - if(filler == NULL) return NULL; filler->width = width; filler->frame = frame; filler->x = width - 1; @@ -297,8 +293,6 @@ static FrameFiller *FrameFiller_new(int width, unsigned char *frame, int mqr) filler->dir = -1; filler->bit = -1; filler->mqr = mqr; - - return filler; } static unsigned char *FrameFiller_next(FrameFiller *filler) @@ -363,30 +357,24 @@ extern unsigned char *FrameFiller_test(int version) { int width; unsigned char *frame, *p; - FrameFiller *filler; int i, length; + FrameFiller filler; width = QRspec_getWidth(version); frame = QRspec_newFrame(version); if(frame == NULL) return NULL; - filler = FrameFiller_new(width, frame, 0); - if(filler == NULL) { - free(frame); - return NULL; - } + FrameFiller_set(&filler, width, frame, 0); length = QRspec_getDataLength(version, QR_ECLEVEL_L) * 8 + QRspec_getECCLength(version, QR_ECLEVEL_L) * 8 + QRspec_getRemainder(version); for(i=0; imqr) { errno = EINVAL; @@ -480,19 +462,14 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask) QRraw_free(raw); return NULL; } - filler = FrameFiller_new(width, frame, 0); - if(filler == NULL) { - QRraw_free(raw); - free(frame); - return NULL; - } + FrameFiller_set(&filler, width, frame, 0); /* inteleaved data and ecc codes */ for(i=0; idataLength + raw->eccLength; i++) { code = QRraw_getCode(raw); bit = 0x80; for(j=0; j<8; j++) { - p = FrameFiller_next(filler); + p = FrameFiller_next(&filler); if(p == NULL) goto EXIT; *p = 0x02 | ((bit & code) != 0); bit = bit >> 1; @@ -503,7 +480,7 @@ __STATIC QRcode *QRcode_encodeMask(QRinput *input, int mask) /* remainder bits */ j = QRspec_getRemainder(version); for(i=0; imqr) { errno = EINVAL; @@ -564,12 +540,7 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) MQRraw_free(raw); return NULL; } - filler = FrameFiller_new(width, frame, 1); - if(filler == NULL) { - MQRraw_free(raw); - free(frame); - return NULL; - } + FrameFiller_set(&filler, width, frame, 1); /* inteleaved data and ecc codes */ for(i=0; idataLength + raw->eccLength; i++) { @@ -577,7 +548,7 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) if(raw->oddbits && i == raw->dataLength - 1) { bit = 1 << (raw->oddbits - 1); for(j=0; joddbits; j++) { - p = FrameFiller_next(filler); + p = FrameFiller_next(&filler); if(p == NULL) goto EXIT; *p = 0x02 | ((bit & code) != 0); bit = bit >> 1; @@ -585,7 +556,7 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) } else { bit = 0x80; for(j=0; j<8; j++) { - p = FrameFiller_next(filler); + p = FrameFiller_next(&filler); if(p == NULL) goto EXIT; *p = 0x02 | ((bit & code) != 0); bit = bit >> 1; @@ -609,7 +580,6 @@ __STATIC QRcode *QRcode_encodeMaskMQR(QRinput *input, int mask) EXIT: MQRraw_free(raw); - free(filler); free(frame); return qrcode; }