]> jfr.im git - irc/quakenet/newserv.git/commitdiff
No code changes to sstring, replace the sstring-old mmap implementation with the...
authorChris Porter <redacted>
Sat, 11 Oct 2008 23:25:15 +0000 (00:25 +0100)
committerChris Porter <redacted>
Sat, 11 Oct 2008 23:25:15 +0000 (00:25 +0100)
Also rename USE_VALGRIND to SSTRING_MMAP in sstring-new.c.

lib/sstring-new.c
lib/sstring-old.c
lib/sstring-valgrind.c [new file with mode: 0644]

index 1c9db86c63acb8ad030cee769cdf78ca69da5dda..88aa5e2f67f413c1f41ad95a8df3cff02869cf5d 100644 (file)
@@ -32,7 +32,7 @@ static int allocs;
 static void sstringstats(int hooknum, void *arg);
 static void salloc(void);
 
-#ifndef USE_VALGRIND
+#ifndef SSTRING_MMAP
 
 #define sunprotect(x)
 #define sunprotectb(x)
@@ -59,7 +59,7 @@ static void *mblock_head;
 #define MAP_ANON MAP_ANONYMOUS
 #endif
 
-#endif /* USE_VALGRIND */
+#endif /* SSTRING_MMAP */
 
 void initsstring() {
   int i;
@@ -80,7 +80,7 @@ void initsstring() {
   registerhook(HOOK_CORE_STATSREQUEST,&sstringstats);
 }
 
-#ifndef USE_VALGRIND
+#ifndef SSTRING_MMAP
 void finisstring() {
   nsfreeall(POOL_SSTRING);
 }
@@ -89,7 +89,7 @@ static void salloc(void) {
   ssmem=(char *)nsmalloc(POOL_SSTRING, SSTRING_ALLOC);
   ssmemfree=SSTRING_ALLOC;
 }
-#endif /* USE_VALGRIND */
+#endif /* SSTRING_MMAP */
 
 sstring *findsstring(const char *str) {
   unsigned int hash=crc32(str)%SSTRING_HASHSIZE;
@@ -118,7 +118,7 @@ void sstring_dehash(sstring *ss) {
       if (!ssp) {
         sshash[hash]=ss->next;
       } else {
-#ifndef USE_VALGRIND
+#ifndef SSTRING_MMAP
         ssp->next=ss->next;
 #else
         if (ssp->block!=ss->block) {
@@ -205,7 +205,7 @@ sstring *getsstring(const char *inputstr, int maxlen) {
       if (ssmemfree>sizeof(sstring)) {
         retval=(sstring *)ssmem;
         sunprotectb(mblock);
-#ifdef USE_VALGRIND
+#ifdef SSTRING_MMAP
         retval->block=mblock;
 #endif
         retval->alloc=(ssmemfree-sizeof(sstring));
@@ -242,7 +242,7 @@ sstring *getsstring(const char *inputstr, int maxlen) {
   strcpy(retval->content,strbuf);
   retval->refcount=1;
   
-#ifdef USE_VALGRIND 
+#ifdef SSTRING_MMAP 
   if(!foreignblock)
     retval->block = mblock;
 #endif
@@ -307,7 +307,7 @@ int sstringcompare(sstring *ss1, sstring *ss2) {
   return strncmp(ss1->content, ss2->content, ss1->length);
 }
 
-#ifdef USE_VALGRIND
+#ifdef SSTRING_MMAP
 void finisstring() {
   struct mblock_list *c, *n;
   for (c=mblock_head;c;c=n) {
@@ -328,4 +328,4 @@ static void salloc(void) {
   ssmem=(char *)mblock + sizeof(struct mblock_list);
   ssmemfree=SSTRING_ALLOC-sizeof(struct mblock_list);
 }
-#endif /* USE_VALGRIND */
+#endif /* SSTRING_MMAP */
index ccb347e1d1a4ad1c81789488fb72067750f18f73..eef029fe44ed19c4f044d4f44d0b68beb7455226 100644 (file)
@@ -14,8 +14,6 @@
 #define __USE_GNU
 #include <string.h>
 
-#ifndef USE_VALGRIND
-
 /* List of free stuff */
 sstring *freelist[SSTRING_MAXLEN+1];
 
@@ -190,116 +188,6 @@ void sstringstats(int hooknum, void *arg) {
   }
 }
 
-#else /* USE_VALGRIND */
-
-#define __USE_MISC
-#include <sys/mman.h>
-
-#ifndef MAP_ANON
-#define MAP_ANON MAP_ANONYMOUS
-#endif
-
-#define MModify(x) mprotect(x, x->s->u.l.alloc, PROT_READ|PROT_WRITE)
-#define MUnmodify(x) mprotect(x, x->s->u.l.alloc, PROT_READ)
-
-typedef struct sstringlist {
-  struct sstringlist *prev;
-  struct sstringlist *next;
-  sstring s[];
-} sstringlist;
-
-static sstringlist *head;
-
-void initsstring() {
-}
-
-void finisstring() {
-  sstringlist *s, *sn;
-
-  /* here we deliberately don't free the pointers so valgrind can tell us where they were allocated, in theory */
-
-  for(s=head;s;s=sn) {
-    sn = s->next;
-
-    MModify(s);
-    s->next = NULL;
-    s->prev = NULL;
-
-    Error("sstring", ERR_WARNING, "sstring of length %d still allocated: %s", s->s->u.l.length, s->s->content);
-  }
-
-  head = NULL;
-}
-
-sstring *getsstring(const char *inputstr, int maxlen) {
-  sstringlist *s;
-  size_t len;
-  char *p;
-  void *m;
-
-  if(!inputstr)
-    return NULL;
-
-  for(p=(char *)inputstr;*p&&maxlen;maxlen--,p++)
-    ; /* empty */
-
-  len = p - inputstr;
-  m=mmap((void *)0, sizeof(sstringlist) + sizeof(sstring) + len + 1, PROT_WRITE|PROT_READ, MAP_PRIVATE|MAP_ANON, -1, 0);
-  s=(sstringlist *)m;
-
-  if(m == MAP_FAILED)
-    s->s->u.l.length = 0;
-
-  s->s->u.l.length = len;
-  s->s->u.l.alloc = sizeof(sstringlist) + sizeof(sstring) + len + 1;
-  s->s->content=(char *)m + sizeof(sstringlist) + sizeof(sstring);
-
-  memcpy(s->s->content, inputstr, len);
-  s->s->content[len] = '\0';
-
-  s->next = head;
-  s->prev = NULL;
-  if(head) {
-    MModify(head);
-    head->prev = s;
-    MUnmodify(head);
-  }
-  head = s;
-
-  MUnmodify(s);
-  return s->s;
-}
-
-void freesstring(sstring *inval) {
-  sstringlist *s;
-  if(!inval)
-    return;
-
-  s = (sstringlist *)inval - 1;
-
-  MModify(s);
-  if(s->prev) {
-    MModify(s->prev);
-    s->prev->next = s->next;
-    MUnmodify(s->prev);
-    if(s->next) {
-      MModify(s->next);
-      s->next->prev = s->prev;
-      MUnmodify(s->prev);
-    }
-  } else {
-    head = s->next;
-    if(head) {
-      MModify(head);
-      head->prev = NULL;
-      MUnmodify(head);
-    }
-  }
-
-  munmap(s, s->s->u.l.alloc);
-}
-#endif
-
 int sstringcompare(sstring *ss1, sstring *ss2) {
   if (ss1->u.l.length != ss2->u.l.length)
     return -1;
diff --git a/lib/sstring-valgrind.c b/lib/sstring-valgrind.c
new file mode 100644 (file)
index 0000000..4a9d8a2
--- /dev/null
@@ -0,0 +1,91 @@
+/* sstring.h - Declaration of "static strings" functions */
+
+#define COMPILING_SSTRING
+#include "sstring.h"
+
+#include "../core/hooks.h"
+#include "../core/nsmalloc.h"
+#include "../core/error.h"
+
+#include <stdio.h>
+
+#include <assert.h>
+#include <stdlib.h>
+#define __USE_GNU
+#include <string.h>
+
+void initsstring() {
+}
+
+void finisstring() {
+  sstringlist *s, *sn;
+
+  /* here we deliberately don't free the pointers so valgrind can tell us where they were allocated, in theory */
+
+  for(s=head;s;s=sn) {
+    sn = s->next;
+    s->next = NULL;
+    s->prev = NULL;
+
+    Error("sstring", ERR_WARNING, "sstring of length %d still allocated: %s", s->s->u.l.length, s->s->content);
+  }
+
+  head = NULL;
+}
+
+sstring *getsstring(const char *inputstr, int maxlen) {
+  sstringlist *s;
+  size_t len;
+  char *p;
+
+  if(!inputstr)
+    return NULL;
+
+  for(p=(char *)inputstr;*p&&maxlen;maxlen--,p++)
+    ; /* empty */
+
+  len = p - inputstr;
+  s=(sstringlist *)malloc(sizeof(sstringlist) + sizeof(sstring));
+
+  s->s->u.l.length = len;
+  s->s->content=(char *)malloc(len + 1);
+
+  memcpy(s->s->content, inputstr, len);
+  s->s->content[len] = '\0';
+
+  s->next = head;
+  s->prev = NULL;
+  if(head)
+    head->prev = s;
+  head = s;
+
+  return s->s;
+}
+
+void freesstring(sstring *inval) {
+  sstringlist *s;
+  if(!inval)
+    return;
+
+  s = (sstringlist *)inval - 1;
+
+  if(s->prev) {
+    s->prev->next = s->next;
+    if(s->next)
+      s->next->prev = s->prev;
+  } else {
+    head = s->next;
+    if(head)
+      head->prev = NULL;
+  }
+
+  free(inval->content);
+  free(s);
+}
+
+int sstringcompare(sstring *ss1, sstring *ss2) {
+  if (ss1->u.l.length != ss2->u.l.length)
+    return -1;
+  
+  return strncmp(ss1->content, ss2->content, ss1->u.l.length);
+}