<pre style='margin:0'>
Joshua Root (jmroot) pushed a commit to branch master
in repository macports-base.
</pre>
<p><a href="https://github.com/macports/macports-base/commit/ee80a08c0f500d569592f5f5667a22b2d09cb05f">https://github.com/macports/macports-base/commit/ee80a08c0f500d569592f5f5667a22b2d09cb05f</a></p>
<pre style="white-space: pre; background: #F8F8F8">The following commit(s) were added to refs/heads/master by this push:
<span style='display:block; white-space:pre;color:#404040;'> new ee80a08c0 strsed: thread safety, modernise code a little
</span>ee80a08c0 is described below
<span style='display:block; white-space:pre;color:#808000;'>commit ee80a08c0f500d569592f5f5667a22b2d09cb05f
</span>Author: Joshua Root <jmr@macports.org>
AuthorDate: Sun Oct 30 11:38:58 2022 +1100
<span style='display:block; white-space:pre;color:#404040;'> strsed: thread safety, modernise code a little
</span>---
src/pextlib1.0/strsed.c | 418 +++++++++++++++++++++---------------------------
1 file changed, 181 insertions(+), 237 deletions(-)
<span style='display:block; white-space:pre;color:#808080;'>diff --git a/src/pextlib1.0/strsed.c b/src/pextlib1.0/strsed.c
</span><span style='display:block; white-space:pre;color:#808080;'>index d8455f489..53df3e6d3 100644
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>--- a/src/pextlib1.0/strsed.c
</span><span style='display:block; white-space:pre;background:#e0e0ff;'>+++ b/src/pextlib1.0/strsed.c
</span><span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -180,25 +180,31 @@
</span> * with the address to be returned.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>-#define RETURN(n) \
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_free(n); \
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+#define RETURN(n, s) \
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_free(n, s); \
</span> if (exp_regs != NULL) \
free(exp_regs); \
return (char *)n
<span style='display:block; white-space:pre;background:#ffe0e0;'>-static struct {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+typedef struct {
</span> char *s;
int size;
int used;
<span style='display:block; white-space:pre;background:#ffe0e0;'>-} mem_slots[MEM_SLOTS];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+} memslot_t;
</span>
<span style='display:block; white-space:pre;background:#e0ffe0;'>+typedef struct {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *what[2];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char last[2];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int increment[2];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int pos[2];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+} nextch_state_t;
</span>
#define more_space(need) \
if (need > 0 && space != -1){ \
if (space - (need) < 0){ \
buf_sz += buf_inc + (need) - space; \
if (!(new_str = (char *)realloc(new_str, (unsigned)buf_sz))){ \
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0); \
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots); \
</span> } \
mem_slots[MEM_NEWSTR].s = new_str; \
mem_slots[MEM_NEWSTR].size = buf_sz; \
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -221,94 +227,81 @@ static struct {
</span> /* ------------------------------------------------------------------------- **
* Prototypes
* ------------------------------------------------------------------------- */
<span style='display:block; white-space:pre;background:#ffe0e0;'>-static char *mem(int, int);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static void mem_init(void);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static void mem_free(char *);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static char *build_map(char *, char *);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static char nextch(char *, int);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static void mem_save(int);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-static int mem_find(int);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *backslash_eliminate(char *, int, int);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static char *mem(int, int, memslot_t*);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static memslot_t *mem_init(void);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static void mem_free(char *, memslot_t*);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static char *build_map(char *, char *, memslot_t*);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static char nextch(char *, int, nextch_state_t *);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static void mem_save(int, memslot_t*);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static int mem_find(int, memslot_t*);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+char *backslash_eliminate(char *, int, int, memslot_t*);
</span>
/* ------------------------------------------------------------------------- **
* strsed
* ------------------------------------------------------------------------- */
char *
<span style='display:block; white-space:pre;background:#ffe0e0;'>-strsed(string, pattern, range)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-register char *string;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-register char *pattern;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int *range;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+strsed(char *string, char *pattern, int *range)
</span> {
#ifdef GNU_REGEX
extern char *re_compile_pattern();
extern int re_search();
<span style='display:block; white-space:pre;background:#ffe0e0;'>- static struct re_pattern_buffer re_comp_buf;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ struct re_pattern_buffer re_comp_buf;
</span> struct re_registers regs;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- static struct re_registers empty_regs;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ struct re_registers empty_regs;
</span> #endif
#ifdef HS_REGEX
<span style='display:block; white-space:pre;background:#ffe0e0;'>- static regmatch_t *exp_regs = NULL;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static regex_t exp;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ regmatch_t *exp_regs = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ regex_t exp;
</span> #endif
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *from;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *new_str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *pat;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *tmp;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *to;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static char map[1 << BYTEWIDTH];
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- int buf_sz;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- int buf_inc;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *from = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *new_str = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *pat = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *str = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *tmp = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *to = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char map[1 << BYTEWIDTH];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int buf_sz = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int buf_inc = 0;
</span> int global = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- int match;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int match = 0;
</span> int new_pos = 0;
int search_only = 0;
int seenbs = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- int space;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int space = 0;
</span> int match_all = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int str_len;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static int first_time = 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static char *last_exp = (char *)0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- int repeat;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char delimiter;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int str_len = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char delimiter = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ memslot_t *mem_slots = NULL;
</span>
if (!string || !pattern){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, NULL);
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> /*
* If this is the first time we've been called, clear the memory slots.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (first_time){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-#ifdef GNU_REGEX
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-#endif
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_init();
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots = mem_init();
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (mem_slots == NULL) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, NULL);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span> #ifdef GNU_REGEX
/* Zero the fake regs that we use if the regex is ".*" */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < RE_NREGS; i++){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < RE_NREGS; i++){
</span> empty_regs.start[i] = empty_regs.end[i] = EMPTY_REGISTER;
}
#endif
<span style='display:block; white-space:pre;background:#ffe0e0;'>-#ifdef HS_REGEX
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- /* We use first_time again if we are GNU_REGEX, and reset it later. */
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- first_time = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-#endif
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span> /*
* Take our own copies of the string and pattern since we promised
* in the man page not to hurt the originals.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- str = mem(MEM_STR, strlen(string) + 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ str = mem(MEM_STR, strlen(string) + 1, mem_slots);
</span> str[0] = '\0';
strcat(str, string);
<span style='display:block; white-space:pre;background:#ffe0e0;'>- pat = mem(MEM_PAT, strlen(pattern) + 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ pat = mem(MEM_PAT, strlen(pattern) + 1, mem_slots);
</span> pat[0] = '\0';
strcat(pat, pattern);
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -318,13 +311,13 @@ int *range;
</span> * or are in any doubt, don't define ESCAPED_STRING.
*/
#ifndef ESCAPED_STRING
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(str = backslash_eliminate(str, NORMAL, MEM_STR))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(str = backslash_eliminate(str, NORMAL, MEM_STR, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
#endif
str_len = strlen(str);
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> /*
* Set up the size of our buffer (in which we build the
* newstring, and the size by which we increment it when
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -373,7 +366,7 @@ int *range;
</span> }
if (!*pat){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
delimiter = *pat++;
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -405,7 +398,7 @@ int *range;
</span> }
if (!*to){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
*to++ = '\0';
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -420,7 +413,7 @@ int *range;
</span> */
if (*tmp != delimiter || *(tmp - 1) == '\\'){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
*tmp = '\0';
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -442,7 +435,7 @@ int *range;
</span> */
if (!range){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
global = 0;
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -456,11 +449,11 @@ int *range;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(from = backslash_eliminate(from, REGEX, MEM_FROM))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(from = backslash_eliminate(from, REGEX, MEM_FROM, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (to && !(to = backslash_eliminate(to, REPLACEMENT, MEM_TO))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (to && !(to = backslash_eliminate(to, REPLACEMENT, MEM_TO, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
/*
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -480,8 +473,8 @@ int *range;
</span> */
if (*to){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(new_str = mem(MEM_NEWSTR, buf_sz + 1))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(new_str = mem(MEM_NEWSTR, buf_sz + 1, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
space = buf_sz;
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -490,29 +483,6 @@ int *range;
</span> space = -1;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- /*
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * Check to see if the regexp is the same as last time.
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * If so, we can save ourselves a call to regexec (or whatever
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * function your regex package uses).
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- *
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- */
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (last_exp){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!strcmp(from, last_exp)){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- repeat = 1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- else{
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- free(last_exp);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- last_exp = strdup(from);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- repeat = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- else {
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- last_exp = strdup(from);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- repeat = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span> /*
* Initialise the range integers to -1, since they may be checked after we
* return, even if we are not just searching.
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -552,23 +522,18 @@ int *range;
</span> * want to do this if the regex is ".*", even if it is our first time.
*/
else{
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (first_time){
</span> if (!(re_comp_buf.buffer = (char *)malloc((unsigned)200))){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> re_comp_buf.allocated = 200;
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> if (!(re_comp_buf.fastmap = (char *)malloc((unsigned)1 << BYTEWIDTH))){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>- first_time = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!repeat){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> re_comp_buf.translate = 0;
re_comp_buf.used = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span> }
#endif
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -577,16 +542,16 @@ int *range;
</span> * compile the regular expression.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!match_all && !repeat){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!match_all){
</span> #ifdef GNU_REGEX
if (re_compile_pattern(from, strlen(from), &re_comp_buf)){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
#endif
#ifdef HS_REGEX
if (regcomp(&exp, from, 0) != 0){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
#endif
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -599,7 +564,7 @@ int *range;
</span> #ifdef HS_REGEX
/* XXX Not even trying to use custom memory routines */
if (!(exp_regs = calloc(str_len, sizeof(regmatch_t)))) {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
#endif
if (match_all){
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -627,17 +592,19 @@ int *range;
</span> #ifdef GNU_REGEX
range[0] = match == NO_MATCH ? -1 : regs.start[0];
range[1] = match == NO_MATCH ? -1 : regs.end[0];
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(re_comp_buf.fastmap);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(re_comp_buf.buffer);
</span> #endif
#ifdef HS_REGEX
range[0] = match == NO_MATCH ? -1 : exp_regs[0].rm_so;
range[1] = match == NO_MATCH ? -1 : exp_regs[0].rm_eo;
#endif
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(str);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(str, mem_slots);
</span> }
if (match != NO_MATCH){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int need;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int need;
</span>
/* Set up the range so it can be used later if the caller wants it. */
if (range){
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -682,7 +649,7 @@ int *range;
</span>
/* A register reference. */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int reg = *(tmp + 1) - '0';
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int reg = *(tmp + 1) - '0';
</span> int translit = 0;
#ifdef GNU_REGEX
need = regs.end[reg] - regs.start[reg];
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -697,8 +664,8 @@ int *range;
</span> */
if (*(tmp + 2) == '{'){
/* A transliteration table. Build the map. */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(tmp = build_map(tmp + 2, map))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(0);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(tmp = build_map(tmp + 2, map, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(NULL, mem_slots);
</span> }
translit = 1;
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -715,7 +682,7 @@ int *range;
</span> */
#ifdef GNU_REGEX
if (regs.start[reg] != EMPTY_REGISTER){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int i;
</span> for (i = regs.start[reg]; i < regs.end[reg]; i++){
new_str[new_pos++] = translit ? map[str[i]] : str[i];
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -724,7 +691,7 @@ int *range;
</span>
#ifdef HS_REGEX
if (exp_regs[0].rm_so != EMPTY_REGISTER){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register regoff_t s;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ regoff_t s;
</span> for (s = exp_regs[0].rm_so; s < exp_regs[0].rm_eo; s++){
new_str[new_pos++] = translit ? map[s] : s;
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -761,16 +728,17 @@ int *range;
</span> */
more_space(str_len);
(void) memmove(new_str + new_pos, str, strlen(str) + 1);
<span style='display:block; white-space:pre;background:#ffe0e0;'>- RETURN(new_str);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+#ifdef GNU_REGEX
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(re_comp_buf.buffer);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(re_comp_buf.fastmap);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+#endif
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ RETURN(new_str, mem_slots);
</span> }
#define DIGIT(x) (isdigit(x) ? (x) - '0' : islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
char *
<span style='display:block; white-space:pre;background:#ffe0e0;'>-backslash_eliminate(str, type, who)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int type;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int who;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+backslash_eliminate(char *str, int type, int who, memslot_t *mem_slots)
</span> {
/*
* Remove backslashes from the strings. Turn \040 etc. into a single
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -817,16 +785,16 @@ int who;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- char *new_str;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *new_str = NULL;
</span> int extra = 100;
int seenlb = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int seenbs = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int i = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int seenbs = 0;
</span> int first_half = 0;
if (type == REPLACEMENT){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(new_str = mem(who, strlen(str) + 1 + extra))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(new_str = mem(who, strlen(str) + 1 + extra, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return NULL;
</span> }
}
else{
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1087,9 +1055,7 @@ int who;
</span> }
static char *
<span style='display:block; white-space:pre;background:#ffe0e0;'>-build_map(s, map)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *s;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *map;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+build_map(char *s, char *map, memslot_t *mem_slots)
</span> {
/*
* Produce a mapping table for the given transliteration.
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1111,16 +1077,14 @@ char *map;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- char *in;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *out;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char *tmp;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- char c;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *in = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *out = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *str = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char *tmp = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ char c = 0;
</span> int i = 0;
int range_count = 0;
int seenbs = 0;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- static char *last = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static int last_len;
</span>
out = 0;
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1128,22 +1092,15 @@ char *map;
</span> return 0;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (last && !strncmp(s, last, last_len)){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- /* Re-use the map. */
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return s + last_len;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- else{
</span> /*
* Make a copy of s in both 'last' and 'str'
*/
int len = strlen(s) + 1;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!(str = mem(MEM_MAP, len)) || !(last = mem(MEM_MAP_SAVE, len))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- return 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!(str = mem(MEM_MAP, len, mem_slots))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return NULL;
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>- str[0] = last[0] = '\0';
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ str[0] = '\0';
</span> strcat(str, s);
<span style='display:block; white-space:pre;background:#ffe0e0;'>- strcat(last, s);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span>
tmp = str + 1;
in = str;
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1202,8 +1159,6 @@ char *map;
</span> return 0;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- last_len = tmp - str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span> /*
* Now 'out' and 'in' both point to character ranges.
* These will look something like "A-Z" but may be
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1219,8 +1174,12 @@ char *map;
</span> * Ready the range expanding function.
*
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- (void) nextch(in, 0);
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- (void) nextch(out, 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ nextch_state_t *nextch_state = calloc(1, sizeof(nextch_state_t));
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (nextch_state == NULL) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ (void) nextch(in, 0, nextch_state);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ (void) nextch(out, 1, nextch_state);
</span>
/*
* For each char in 'in', assign it a value in
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1228,17 +1187,16 @@ char *map;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- while ((c = nextch((char *)0, 0))){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- map[(int) c] = nextch((char *)0, 1);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ while ((c = nextch((char *)0, 0, nextch_state))){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ map[(int) c] = nextch((char *)0, 1, nextch_state);
</span> }
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(nextch_state);
</span>
return tmp;
}
static char
<span style='display:block; white-space:pre;background:#ffe0e0;'>-nextch(str, who)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int who;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+nextch(char *str, int who, nextch_state_t *state)
</span> {
/*
* Given a range like {a-z0237-9}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1264,38 +1222,33 @@ int who;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- static char *what[2] = {0, 0};
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static char last[2] = {0, 0};
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static int increment[2];
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- static int pos[2];
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span> if (who < 0 || who > 1){
return 0;
}
if (str){
/* Set up for this string. */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- what[who] = str;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pos[who] = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->what[who] = str;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->pos[who] = 0;
</span> return 1;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- else if (!what[who]){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ else if (!state->what[who]){
</span> return 0;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!pos[who] && what[who][0] == '-'){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!state->pos[who] && state->what[who][0] == '-'){
</span> return 0;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- switch (what[who][pos[who]]){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ switch (state->what[who][state->pos[who]]){
</span>
case '-':{
/* we're in mid-range. */
<span style='display:block; white-space:pre;background:#ffe0e0;'>- last[who] += increment[who];
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (what[who][pos[who] + 1] == last[who]){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pos[who] += 2;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->last[who] += state->increment[who];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (state->what[who][state->pos[who] + 1] == state->last[who]){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->pos[who] += 2;
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return last[who];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return state->last[who];
</span> }
case '\0':{
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1304,18 +1257,18 @@ int who;
</span> * last thing you saw if who = 1.
*/
if (who){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return last[1];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return state->last[1];
</span> }
return 0;
}
/* FALLTHROUGH */
case '\\':{
<span style='display:block; white-space:pre;background:#ffe0e0;'>- pos[who]++;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->pos[who]++;
</span> }
default:{
<span style='display:block; white-space:pre;background:#ffe0e0;'>- last[who] = what[who][pos[who]++];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->last[who] = state->what[who][state->pos[who]++];
</span> /*
* If we have reached a '-' then this is the start of a
* range. Keep on moving forward until we see a sensible
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1325,41 +1278,39 @@ int who;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- while (what[who][pos[who]] == '-'){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ while (state->what[who][state->pos[who]] == '-'){
</span> int inc = 1;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (what[who][pos[who] + inc] == '\\'){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (state->what[who][state->pos[who] + inc] == '\\'){
</span> inc++;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!what[who][pos[who] + inc]){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!state->what[who][state->pos[who] + inc]){
</span> return 0;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (what[who][pos[who] + inc + 1] == '-'){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pos[who] += inc + 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (state->what[who][state->pos[who] + inc + 1] == '-'){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->pos[who] += inc + 1;
</span> continue;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- increment[who] = what[who][pos[who] + inc] - last[who];
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (!increment[who]){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- pos[who] += 2;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->increment[who] = state->what[who][state->pos[who] + inc] - state->last[who];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (!state->increment[who]){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->pos[who] += 2;
</span> continue;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- if (increment[who] > 0){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- increment[who] = 1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (state->increment[who] > 0){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->increment[who] = 1;
</span> break;
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- else if (increment[who] < 0){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- increment[who] = -1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ else if (state->increment[who] < 0){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ state->increment[who] = -1;
</span> break;
}
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return last[who];
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return state->last[who];
</span> }
}
}
static char *
<span style='display:block; white-space:pre;background:#ffe0e0;'>-mem(who, size)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int who;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int size;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+mem(int who, int size, memslot_t *mem_slots)
</span> {
/*
* Get 'size' bytes of memory one way or another.
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1372,7 +1323,7 @@ int size;
</span> */
if (who < 0 || who >= MEM_SLOTS){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- return 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return NULL;
</span> }
if (mem_slots[who].used){
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1385,7 +1336,7 @@ int size;
</span> return mem_slots[who].s;
}
else{
<span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_save(who);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_save(who, mem_slots);
</span> }
}
else{
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1403,15 +1354,17 @@ int size;
</span> }
if (mem_slots[who].s){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_save(who);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_save(who, mem_slots);
</span> }
else{
<span style='display:block; white-space:pre;background:#ffe0e0;'>- int x = mem_find(size);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int x = mem_find(size, mem_slots);
</span> if (x != -1){
mem_slots[who].s = mem_slots[x].s;
mem_slots[who].size = mem_slots[x].size;
mem_slots[who].used = 1;
<span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_slots[x].s = (char *)0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].s = (char *)NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].size = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].used = 0;
</span> return mem_slots[who].s;
}
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1431,17 +1384,14 @@ int size;
</span> }
static int
<span style='display:block; white-space:pre;background:#ffe0e0;'>-mem_find(size)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int size;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+mem_find(int size, memslot_t *mem_slots)
</span> {
/*
* See if we can find an unused but allocated slot with 'size'
* (or more) space available. Return the index, or -1 if not.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span>
<span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < MEM_SLOTS; i++){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < MEM_SLOTS; i++){
</span> if (!mem_slots[i].used && mem_slots[i].s && mem_slots[i].size >= size){
return i;
}
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1450,8 +1400,7 @@ int size;
</span> }
static void
<span style='display:block; white-space:pre;background:#ffe0e0;'>-mem_save(x)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-int x;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+mem_save(int x, memslot_t *mem_slots)
</span> {
/*
* There is some memory in mem_slots[x] and we try to save it rather
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1463,37 +1412,39 @@ int x;
</span> *
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int saved = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int saved = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> /*
* First we try to find somewhere unused and with no present allocation.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < MEM_SLOTS; i++){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < MEM_SLOTS; i++) {
</span> if (!mem_slots[i].used && !mem_slots[i].s){
saved = 1;
mem_slots[i].s = mem_slots[x].s;
mem_slots[i].size = mem_slots[x].size;
mem_slots[i].used = 0;
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].s = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].size = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].used = 0;
</span> break;
}
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> /*
* No luck yet. Try for a place that is not being used but which has
* space allocated, and which is smaller than us (and all other such spots).
* Pick on the smallest, yeah.
*/
if (!saved){
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int small = -1;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int small_val = 32767; /* Be nice to 16 bit'ers. Non-crucial if it's too low. */
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < MEM_SLOTS; i++){
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int small = -1;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ int small_val = 32767; /* Be nice to 16 bit'ers. Non-crucial if it's too low. */
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < MEM_SLOTS; i++) {
</span> if (!mem_slots[i].used && mem_slots[i].size < mem_slots[x].size && mem_slots[i].size < small_val){
small_val = mem_slots[i].size;
small = i;
}
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> if (small != -1){
saved = 1;
/* We got one, now clobber it... */
<span style='display:block; white-space:pre;background:#e0e0e0;'>@@ -1502,55 +1453,48 @@ int x;
</span> mem_slots[small].s = mem_slots[x].s;
mem_slots[small].size = mem_slots[x].size;
mem_slots[small].used = 0;
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].s = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].size = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].used = 0;
</span> }
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span> if (!saved){
/* Have to toss it away. */
free(mem_slots[x].s);
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].s = NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].size = 0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[x].used = 0;
</span> }
}
<span style='display:block; white-space:pre;background:#ffe0e0;'>-static void
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-mem_init()
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+static memslot_t *
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+mem_init(void)
</span> {
/*
* Clear all the memory slots.
*/
<span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < MEM_SLOTS; i++){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_slots[i].s = (char *)0;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ memslot_t *mem_slots = malloc(MEM_SLOTS * sizeof(memslot_t));
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (mem_slots == NULL) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ return NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < MEM_SLOTS; i++) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[i].s = (char *)NULL;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ mem_slots[i].size = 0;
</span> mem_slots[i].used = 0;
}
<span style='display:block; white-space:pre;background:#e0ffe0;'>+ return mem_slots;
</span> }
static void
<span style='display:block; white-space:pre;background:#ffe0e0;'>-mem_free(except)
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-char *except;
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+mem_free(char *except, memslot_t *mem_slots)
</span> {
<span style='display:block; white-space:pre;background:#ffe0e0;'>- /*
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * "Clear out" all the memory slots. Actually we do no freeing since
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * we may well be called again. We just mark the slots as unused. Next
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * time round they might be useful - the addresses and sizes are still there.
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- *
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * For the slot (if any) whose address is 'except', we actually set the
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * address to 0. This is done because we are called ONLY from the macro
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * RETURN() in strsed() and we intend to return the value in 'except'.
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * Once this is done, strsed should (in theory) have no knowledge at all
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * of the address it passed back last time. That way we won't clobber it
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- * and cause all sorts of nasty problems.
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- */
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- register int i;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- for (i = 0; i < MEM_SLOTS; i++){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_slots[i].used = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- if (mem_slots[i].s == except){
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_slots[i].s = (char *)0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- mem_slots[i].size = 0;
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#ffe0e0;'>- }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ for (int i = 0; i < MEM_SLOTS; i++) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ if (mem_slots[i].s != NULL && mem_slots[i].s != except) {
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(mem_slots[i].s);
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ }
</span><span style='display:block; white-space:pre;background:#e0ffe0;'>+ free(mem_slots);
</span> }
<span style='display:block; white-space:pre;background:#ffe0e0;'>-
</span></pre><pre style='margin:0'>
</pre>