Skip to content

Commit

Permalink
Frame caches have been eliminated.
Browse files Browse the repository at this point in the history
  • Loading branch information
fukuchi committed Sep 9, 2014
1 parent bf0ec2e commit b650d72
Show file tree
Hide file tree
Showing 15 changed files with 12 additions and 133 deletions.
10 changes: 10 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@
* mask.[ch], tests/test_mask.c:
- Run length calculation has been slightly improved.
- Reduce malloc calls.
[noframecache]
* qrspec.[ch], mqrspec.[ch]:
- Frame caches have been eliminated. It improves both memory efficiency
and performance... Wait, caches were completely meaningless? orz...
* qrencode.[ch]:
- QRcode_clearCache() has been eliminated.
* tests/prof_qrencode.c, tests/pthread_qrencode.c, tests/test_mask.c,
tests/test_mmask.c, tests/test_monkey.c, tests/test_mqrspec.c,
tests/test_qrencode.c, tests/test_qrspec.c:
- Removed cache clearing calls.

2014.09.08 Kentaro FUKUCHI <kentaro@fukuchi.org>
* qrenc.c:
Expand Down
47 changes: 1 addition & 46 deletions mqrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,6 @@ unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level)
* Frame
*****************************************************************************/

/**
* Cache of initial frames.
*/
/* C99 says that static storage shall be initialized to a null pointer
* by compiler. */
static unsigned char *frames[MQRSPEC_VERSION_MAX + 1];
#ifdef HAVE_LIBPTHREAD
static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

/**
* Put a finder pattern.
* @param frame
Expand Down Expand Up @@ -239,42 +229,7 @@ static unsigned char *MQRspec_createFrame(int version)

unsigned char *MQRspec_newFrame(int version)
{
unsigned char *frame;
int width;

if(version < 1 || version > MQRSPEC_VERSION_MAX) return NULL;

#ifdef HAVE_LIBPTHREAD
pthread_mutex_lock(&frames_mutex);
#endif
if(frames[version] == NULL) {
frames[version] = MQRspec_createFrame(version);
}
#ifdef HAVE_LIBPTHREAD
pthread_mutex_unlock(&frames_mutex);
#endif
if(frames[version] == NULL) return NULL;

width = mqrspecCapacity[version].width;
frame = (unsigned char *)malloc(width * width);
if(frame == NULL) return NULL;
memcpy(frame, frames[version], width * width);

return frame;
}

void MQRspec_clearCache(void)
{
int i;

#ifdef HAVE_LIBPTHREAD
pthread_mutex_lock(&frames_mutex);
#endif
for(i=1; i<=MQRSPEC_VERSION_MAX; i++) {
free(frames[i]);
frames[i] = NULL;
}
#ifdef HAVE_LIBPTHREAD
pthread_mutex_unlock(&frames_mutex);
#endif
return MQRspec_createFrame(version);
}
5 changes: 0 additions & 5 deletions mqrspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ extern unsigned int MQRspec_getFormatInfo(int mask, int version, QRecLevel level
*/
extern unsigned char *MQRspec_newFrame(int version);

/**
* Clear the frame cache. Typically for debug.
*/
extern void MQRspec_clearCache(void);

/******************************************************************************
* Mode indicator
*****************************************************************************/
Expand Down
6 changes: 0 additions & 6 deletions qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -911,9 +911,3 @@ char *QRcode_APIVersionString(void)
{
return VERSION;
}

void QRcode_clearCache(void)
{
QRspec_clearCache();
MQRspec_clearCache();
}
7 changes: 0 additions & 7 deletions qrencode.h
Original file line number Diff line number Diff line change
Expand Up @@ -552,13 +552,6 @@ extern void QRcode_APIVersion(int *major_version, int *minor_version, int *micro
*/
extern char *QRcode_APIVersionString(void);

/**
* Clear all caches. This is only for debug purpose. If you are attacking a
* complicated memory leak bug, try this to reduce the reachable blocks record.
* @warning This function is THREAD UNSAFE when pthread is disabled.
*/
extern void QRcode_clearCache(void);

#if defined(__cplusplus)
}
#endif
Expand Down
47 changes: 1 addition & 46 deletions qrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,6 @@ unsigned int QRspec_getFormatInfo(int mask, QRecLevel level)
* Frame
*****************************************************************************/

/**
* Cache of initial frames.
*/
/* C99 says that static storage shall be initialized to a null pointer
* by compiler. */
static unsigned char *frames[QRSPEC_VERSION_MAX + 1];
#ifdef HAVE_LIBPTHREAD
static pthread_mutex_t frames_mutex = PTHREAD_MUTEX_INITIALIZER;
#endif

/**
* Put a finder pattern.
* @param frame
Expand Down Expand Up @@ -521,42 +511,7 @@ static unsigned char *QRspec_createFrame(int version)

unsigned char *QRspec_newFrame(int version)
{
unsigned char *frame;
int width;

if(version < 1 || version > QRSPEC_VERSION_MAX) return NULL;

#ifdef HAVE_LIBPTHREAD
pthread_mutex_lock(&frames_mutex);
#endif
if(frames[version] == NULL) {
frames[version] = QRspec_createFrame(version);
}
#ifdef HAVE_LIBPTHREAD
pthread_mutex_unlock(&frames_mutex);
#endif
if(frames[version] == NULL) return NULL;

width = qrspecCapacity[version].width;
frame = (unsigned char *)malloc(width * width);
if(frame == NULL) return NULL;
memcpy(frame, frames[version], width * width);

return frame;
}

void QRspec_clearCache(void)
{
int i;

#ifdef HAVE_LIBPTHREAD
pthread_mutex_lock(&frames_mutex);
#endif
for(i=1; i<=QRSPEC_VERSION_MAX; i++) {
free(frames[i]);
frames[i] = NULL;
}
#ifdef HAVE_LIBPTHREAD
pthread_mutex_unlock(&frames_mutex);
#endif
return QRspec_createFrame(version);
}
7 changes: 0 additions & 7 deletions qrspec.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,11 @@ extern unsigned int QRspec_getFormatInfo(int mask, QRecLevel level);

/**
* Return a copy of initialized frame.
* When the same version is requested twice or more, a copy of cached frame
* is returned.
* @param version
* @return Array of unsigned char. You can free it by free().
*/
extern unsigned char *QRspec_newFrame(int version);

/**
* Clear the frame cache. Typically for debug.
*/
extern void QRspec_clearCache(void);

/******************************************************************************
* Mode indicator
*****************************************************************************/
Expand Down
2 changes: 0 additions & 2 deletions tests/prof_qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,5 @@ int main(int argc, char **argv)
prof_ver1to10();
prof_ver31to40();

QRcode_clearCache();

return 0;
}
2 changes: 0 additions & 2 deletions tests/pthread_qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,5 @@ int main(int argc, char **argv)
prof_ver1to10();
prof_ver31to40();

QRcode_clearCache();

return 0;
}
2 changes: 0 additions & 2 deletions tests/test_mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,5 @@ int main(int argc, char **argv)

report();

QRspec_clearCache();

return 0;
}
2 changes: 0 additions & 2 deletions tests/test_mmask.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,5 @@ int main(int argc, char **argv)

report();

MQRspec_clearCache();

return 0;
}
2 changes: 0 additions & 2 deletions tests/test_monkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,5 @@ int main(int argc, char **argv)
monkey_encode_kanji(loop);
monkey_split_structure(loop);

QRcode_clearCache();

return 0;
}
2 changes: 0 additions & 2 deletions tests/test_mqrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ int main(int argc, char **argv)
test_format();
test_dataLength();

MQRspec_clearCache();

report();

return 0;
Expand Down
2 changes: 0 additions & 2 deletions tests/test_qrencode.c
Original file line number Diff line number Diff line change
Expand Up @@ -994,8 +994,6 @@ int main(int argc, char **argv)
test_mqrencode();
test_apiversion();

QRcode_clearCache();

report();

return 0;
Expand Down
2 changes: 0 additions & 2 deletions tests/test_qrspec.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ int main(int argc, char **argv)
//print_newFrame();
test_format();

QRspec_clearCache();

report();

return 0;
Expand Down

0 comments on commit b650d72

Please sign in to comment.