Skip to content

Commit

Permalink
Fix: Rewritre sys realloc function
Browse files Browse the repository at this point in the history
  • Loading branch information
MedourMehdi committed Oct 11, 2023
1 parent 3deaff0 commit 12fe5b3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
44 changes: 43 additions & 1 deletion goo/gmem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Copyright 1996-2003 Glyph & Cog, LLC
*/

#include "../aconf.h"
#include "../aconf.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
Expand All @@ -22,6 +22,16 @@
#endif
#include "../goo/gmem.h"

#ifdef __MINT__
#include "../mint_alloc.h"
#endif

// #ifdef __MINT__

// #else

// #endif

#ifdef DEBUG_MEM

typedef struct _GMemHdr {
Expand Down Expand Up @@ -145,9 +155,15 @@ void *gmalloc(int size) GMEM_EXCEP {
if (size == 0) {
return NULL;
}
#ifdef __MINT__
if (!(p = gmint_malloc(size))) {
gMemError("Out of memory");
}
#else
if (!(p = malloc(size))) {
gMemError("Out of memory");
}
#endif
return p;
}
#endif
Expand Down Expand Up @@ -185,15 +201,27 @@ void *grealloc(void *p, int size) GMEM_EXCEP {
}
if (size == 0) {
if (p) {
#ifdef __MINT__
gmint_free(p);
#else
free(p);
#endif
}
return NULL;
}
#ifdef __MINT__
if (p) {
q = gmint_realloc(p, size);
} else {
q = gmint_malloc(size);
}
#else
if (p) {
q = realloc(p, size);
} else {
q = malloc(size);
}
#endif
if (!q) {
gMemError("Out of memory");
}
Expand Down Expand Up @@ -268,9 +296,16 @@ void *gmalloc64(size_t size) GMEM_EXCEP {
if (size == 0) {
return NULL;
}
#ifdef __MINT__
if (!(p = gmint_malloc(size))) {
gMemError("Out of memory");
}
#else
if (!(p = malloc(size))) {
gMemError("Out of memory");
}
#endif

return p;
}
#endif
Expand Down Expand Up @@ -344,11 +379,18 @@ void gfree(void *p) {
fprintf(stderr, "Attempted to free bad address %p\n", p);
}
}
#else
#ifdef __MINT__
if (p) {
gmint_free(p);
}
#else
if (p) {
free(p);
}
#endif

#endif
}

void gMemError(const char *msg) GMEM_EXCEP {
Expand Down
41 changes: 41 additions & 0 deletions mint_alloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mintbind.h>

void gmint_free(void * p);
void * gmint_malloc(size_t n);
size_t gmint_getsize(void * p);
void *gmint_realloc(void *ptr,size_t size);

void gmint_free(void * p) {
size_t * in = (size_t *)p;
if (in) {
--in; Mfree(in);
}
}

void * gmint_malloc(size_t n) {
size_t * result = (size_t *)Mxalloc(n + sizeof(size_t), 3);
if (result) { *result = n; ++result; memset(result,0,n); }
return result;
}

size_t gmint_getsize(void * p) {
size_t * in = (size_t *)p;
if (in) { --in; return *in; }
return -1;
}

void *gmint_realloc(void *ptr,size_t size) {
void *newptr;
int msize;
msize = gmint_getsize(ptr);
// printf("msize=%d\n", msize);
if (size <= msize)
return ptr;
newptr = gmint_malloc(size);
memcpy(newptr, ptr, msize);
gmint_free(ptr);
return newptr;
}
13 changes: 8 additions & 5 deletions xpdf/CharCodeToUnicode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,16 @@ void CharCodeToUnicode::setMapping(CharCode c, Unicode *u, int len) {
int CharCodeToUnicode::mapToUnicode(CharCode c, Unicode *u, int size) {
int i, j;

if (!map) {
u[0] = (Unicode)c;
return 1;
}
if (c >= mapLen) {
/*Add returned flag for no mapping found.*/
// if (!map) {
// u[0] = (Unicode)c;
// return 1;
// }
if (c >= mapLen || !map) {
return 0;
}
//AA : when no mapping 0 is default, to be catched later for recognition
u[0] = map[c];
if (map[c]) {
u[0] = map[c];
return 1;
Expand Down
4 changes: 3 additions & 1 deletion xpdf/GlobalParams.cc
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,9 @@ GlobalParams::GlobalParams(const char *cfgFileName) {
launchCommand = NULL;
movieCommand = NULL;
defaultPrinter = NULL;
mapNumericCharNames = gTrue;
/*Deactivate mappings using numeric charnames since they are not reliable and use unverified heuristic to be resolved. */
// mapNumericCharNames = gTrue;
mapNumericCharNames = gFalse;
mapUnknownCharNames = gFalse;
mapExtTrueTypeFontsViaUnicode = gTrue;
useTrueTypeUnicodeMapping = gFalse;
Expand Down

0 comments on commit 12fe5b3

Please sign in to comment.