]>
jfr.im git - irc/quakenet/newserv.git/blob - lib/sstring-old.c
1 /* sstring.h - Declaration of "static strings" functions */
3 #define COMPILING_SSTRING
6 #include "../core/hooks.h"
7 #include "../core/nsmalloc.h"
8 #include "../core/error.h"
17 /* List of free stuff */
18 sstring
*freelist
[SSTRING_MAXLEN
+1];
20 /* Global variables to track allocated memory */
26 /* Statistics counters */
32 /* Internal function */
33 void sstringstats(int hooknum
, void *arg
);
37 for(i
=0;i
<=SSTRING_MAXLEN
;i
++)
45 /* Initialise statistics counters */
51 registerhook(HOOK_CORE_STATSREQUEST
,&sstringstats
);
55 nsfreeall(POOL_SSTRING
);
58 sstring
*getsstring(const char *inputstr
, int maxlen
) {
64 /* getsstring() on a NULL pointer returns a NULL sstring.. */
69 if (inputstr
[0]=='\0') {
73 /* Only count calls that actually did something */
76 length
=strlen(inputstr
)+1;
80 assert(length
<=SSTRING_MAXLEN
);
82 /* Check to see if an approximately correct
83 * sized string is available */
84 for(i
=0;i
<SSTRING_SLACK
;i
++) {
85 if (length
+i
>SSTRING_MAXLEN
)
88 if (freelist
[length
+i
]!=NULL
) {
89 retval
=freelist
[length
+i
];
90 freelist
[length
+i
]=retval
->u
.next
;
91 retval
->u
.l
.alloc
=(length
+i
);
96 /* None found, allocate a new one */
99 if (structfree
< sizeof(sstring
)) {
100 /* We always allocate an exact multiple of these.
101 * Therefore, if there is enough for a partial structure we broke something */
102 assert(structfree
==0);
104 /* Allocate more RAM */
106 structmem
=(sstring
*)nsmalloc(POOL_SSTRING
,SSTRING_STRUCTALLOC
);
107 assert(structmem
!=NULL
);
108 structfree
=SSTRING_STRUCTALLOC
;
113 structfree
-=sizeof(sstring
);
115 if (stringfree
< length
) {
116 /* Not enough left for what we want.
117 * Allocate the remainder of our chunk (if any)
118 * to something and immediately free it.
119 * Decrement the freecalls counter to fix the stats */
120 if (stringfree
> 0) {
121 retval
->content
=stringmem
;
122 retval
->u
.l
.alloc
=stringfree
;
127 /* GOTO GOTO GOTO: We need to allocate
128 * another new struct here. Goto is the cleanest
132 /* Grab some more string space */
134 stringmem
=(char *)nsmalloc(POOL_SSTRING
,SSTRING_DATAALLOC
);
135 assert(stringmem
!=NULL
);
136 stringfree
=SSTRING_DATAALLOC
;
140 retval
->content
=stringmem
;
141 retval
->u
.l
.alloc
=length
;
147 * At this point, retval points to a valid structure which is at
148 * least the right size and has the "alloc" value set correctly
151 retval
->u
.l
.length
=(length
-1);
152 strncpy(retval
->content
,inputstr
,(length
-1));
153 retval
->content
[length
-1]='\0';
158 void freesstring(sstring
*inval
) {
161 /* Allow people to call this with a NULL value */
165 /* Only count calls that actually did something */
168 alloc
=inval
->u
.l
.alloc
;
169 assert(alloc
<=SSTRING_MAXLEN
);
170 inval
->u
.next
=freelist
[alloc
];
171 freelist
[alloc
]=inval
;
174 void sstringstats(int hooknum
, void *arg
) {
178 long statslev
=(long)arg
;
181 for(i
=0,j
=0;i
<=SSTRING_MAXLEN
;i
++) {
182 for(ssp
=freelist
[i
];ssp
;ssp
=ssp
->u
.next
)
186 snprintf(buf
,512,"SString : %7d get calls, %7d free calls, %7d struct allocs, %7d string allocs, %7d strings free",getcalls
,freecalls
,allocstruct
,allocstring
,j
);
187 triggerhook(HOOK_CORE_STATSREPLY
,(void *)buf
);