1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
|
/*
* Copyright (c) 2000, 2001 Ximian Inc.
*
* Authors: Michael Zucchi <notzed@ximian.com>
* Jacob Berkman <jacob@ximian.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include "e-memory.h"
#include <string.h> /* memset() */
#include <stdlib.h> /* alloca() */
#include <glib.h>
#define s(x) /* strv debug */
#define p(x) /* poolv debug */
#define p2(x) /* poolv assertion checking */
/*#define MALLOC_CHECK*/
/*#define PROFILE_POOLV*/
#ifdef PROFILE_POOLV
#include <time.h>
#define pp(x) x
#else
#define pp(x)
#endif
/*#define TIMEIT*/
#ifdef TIMEIT
#include <sys/time.h>
#include <unistd.h>
struct timeval timeit_start;
static time_start(const char *desc)
{
gettimeofday(&timeit_start, NULL);
printf("starting: %s\n", desc);
}
static time_end(const char *desc)
{
unsigned long diff;
struct timeval end;
gettimeofday(&end, NULL);
diff = end.tv_sec * 1000 + end.tv_usec/1000;
diff -= timeit_start.tv_sec * 1000 + timeit_start.tv_usec/1000;
printf("%s took %ld.%03ld seconds\n",
desc, diff / 1000, diff % 1000);
}
#else
#define time_start(x)
#define time_end(x)
#endif
#ifdef MALLOC_CHECK
#include <mcheck.h>
#include <stdio.h>
static void
checkmem(void *p)
{
if (p) {
int status = mprobe(p);
switch (status) {
case MCHECK_HEAD:
printf("Memory underrun at %p\n", p);
abort();
case MCHECK_TAIL:
printf("Memory overrun at %p\n", p);
abort();
case MCHECK_FREE:
printf("Double free %p\n", p);
abort();
}
}
}
#define MPROBE(x) checkmem((void *)(x))
#else
#define MPROBE(x)
#endif
/* mempool class */
typedef struct _MemChunkFreeNode {
struct _MemChunkFreeNode *next;
unsigned int atoms;
} MemChunkFreeNode;
typedef struct _EMemChunk {
unsigned int blocksize; /* number of atoms in a block */
unsigned int atomsize; /* size of each atom */
GPtrArray *blocks; /* blocks of raw memory */
struct _MemChunkFreeNode *free;
} MemChunk;
/**
* e_memchunk_new:
* @atomcount: The number of atoms stored in a single malloc'd block of memory.
* @atomsize: The size of each allocation.
*
* Create a new memchunk header. Memchunks are an efficient way to allocate
* and deallocate identical sized blocks of memory quickly, and space efficiently.
*
* e_memchunks are effectively the same as gmemchunks, only faster (much), and
* they use less memory overhead for housekeeping.
*
* Return value: The new header.
**/
MemChunk *e_memchunk_new(int atomcount, int atomsize)
{
MemChunk *m = g_malloc(sizeof(*m));
m->blocksize = atomcount;
m->atomsize = MAX(atomsize, sizeof(MemChunkFreeNode));
m->blocks = g_ptr_array_new();
m->free = NULL;
return m;
}
/**
* memchunk_alloc:
* @m:
*
* Allocate a new atom size block of memory from a memchunk.
**/
void *e_memchunk_alloc(MemChunk *m)
{
char *b;
MemChunkFreeNode *f;
void *mem;
f = m->free;
if (f) {
f->atoms--;
if (f->atoms > 0) {
mem = ((char *)f) + (f->atoms*m->atomsize);
} else {
mem = f;
m->free = m->free->next;
}
return mem;
} else {
b = g_malloc(m->blocksize * m->atomsize);
g_ptr_array_add(m->blocks, b);
f = (MemChunkFreeNode *)&b[m->atomsize];
f->atoms = m->blocksize-1;
f->next = NULL;
m->free = f;
return b;
}
}
void *e_memchunk_alloc0(EMemChunk *m)
{
void *mem;
mem = e_memchunk_alloc(m);
memset(mem, 0, m->atomsize);
return mem;
}
/**
* e_memchunk_free:
* @m:
* @mem: Address of atom to free.
*
* Free a single atom back to the free pool of atoms in the given
* memchunk.
**/
void
e_memchunk_free(MemChunk *m, void *mem)
{
MemChunkFreeNode *f;
/* put the location back in the free list. If we knew if the preceeding or following
cells were free, we could merge the free nodes, but it doesn't really add much */
f = mem;
f->next = m->free;
m->free = f;
f->atoms = 1;
/* we could store the free list sorted - we could then do the above, and also
probably improve the locality of reference properties for the allocator */
/* and it would simplify some other algorithms at that, but slow this one down
significantly */
}
/**
* e_memchunk_empty:
* @m:
*
* Clean out the memchunk buffers. Marks all allocated memory as free blocks,
* but does not give it back to the system. Can be used if the memchunk
* is to be used repeatedly.
**/
void
e_memchunk_empty(MemChunk *m)
{
int i;
MemChunkFreeNode *f, *h = NULL;
for (i=0;i<m->blocks->len;i++) {
f = (MemChunkFreeNode *)m->blocks->pdata[i];
f->atoms = m->blocksize;
f->next = h;
h = f;
}
m->free = h;
}
struct _cleaninfo {
struct _cleaninfo *next;
char *base;
int count;
int size; /* just so tree_search has it, sigh */
};
static int tree_compare(struct _cleaninfo *a, struct _cleaninfo *b)
{
if (a->base < b->base)
return -1;
else if (a->base > b->base)
return 1;
return 0;
}
static int tree_search(struct _cleaninfo *a, char *mem)
{
if (a->base <= mem) {
if (mem < &a->base[a->size])
return 0;
return 1;
}
return -1;
}
/**
* e_memchunk_clean:
* @m:
*
* Scan all empty blocks and check for blocks which can be free'd
* back to the system.
*
* This routine may take a while to run if there are many allocated
* memory blocks (if the total number of allocations is many times
* greater than atomcount).
**/
void
e_memchunk_clean(MemChunk *m)
{
GTree *tree;
int i;
MemChunkFreeNode *f;
struct _cleaninfo *ci, *hi = NULL;
f = m->free;
if (m->blocks->len == 0 || f == NULL)
return;
/* first, setup the tree/list so we can map free block addresses to block addresses */
tree = g_tree_new((GCompareFunc)tree_compare);
for (i=0;i<m->blocks->len;i++) {
ci = alloca(sizeof(*ci));
ci->count = 0;
ci->base = m->blocks->pdata[i];
ci->size = m->blocksize * m->atomsize;
g_tree_insert(tree, ci, ci);
ci->next = hi;
hi = ci;
}
/* now, scan all free nodes, and count them in their tree node */
while (f) {
ci = g_tree_search(tree, (GCompareFunc) tree_search, f);
if (ci) {
ci->count += f->atoms;
} else {
g_warning("error, can't find free node in memory block\n");
}
f = f->next;
}
/* if any nodes are all free, free & unlink them */
ci = hi;
while (ci) {
if (ci->count == m->blocksize) {
MemChunkFreeNode *prev = NULL;
f = m->free;
while (f) {
if (tree_search (ci, (void *) f) == 0) {
/* prune this node from our free-node list */
if (prev)
prev->next = f->next;
else
m->free = f->next;
} else {
prev = f;
}
f = f->next;
}
g_ptr_array_remove_fast(m->blocks, ci->base);
g_free(ci->base);
}
ci = ci->next;
}
g_tree_destroy(tree);
}
/**
* e_memchunk_destroy:
* @m:
*
* Free the memchunk header, and all associated memory.
**/
void
e_memchunk_destroy(MemChunk *m)
{
int i;
if (m == NULL)
return;
for (i=0;i<m->blocks->len;i++)
g_free(m->blocks->pdata[i]);
g_ptr_array_free(m->blocks, TRUE);
g_free(m);
}
typedef struct _MemPoolNode {
struct _MemPoolNode *next;
int free;
} MemPoolNode;
typedef struct _MemPoolThresholdNode {
struct _MemPoolThresholdNode *next;
} MemPoolThresholdNode;
#define ALIGNED_SIZEOF(t) ((sizeof (t) + G_MEM_ALIGN - 1) & -G_MEM_ALIGN)
typedef struct _EMemPool {
int blocksize;
int threshold;
unsigned int align;
struct _MemPoolNode *blocks;
struct _MemPoolThresholdNode *threshold_blocks;
} MemPool;
/* a pool of mempool header blocks */
static MemChunk *mempool_memchunk;
#ifdef G_THREADS_ENABLED
static GStaticMutex mempool_mutex = G_STATIC_MUTEX_INIT;
#endif
/**
* e_mempool_new:
* @blocksize: The base blocksize to use for all system alocations.
* @threshold: If the allocation exceeds the threshold, then it is
* allocated separately and stored in a separate list.
* @flags: Alignment options: E_MEMPOOL_ALIGN_STRUCT uses native
* struct alignment, E_MEMPOOL_ALIGN_WORD aligns to 16 bits (2 bytes),
* and E_MEMPOOL_ALIGN_BYTE aligns to the nearest byte. The default
* is to align to native structures.
*
* Create a new mempool header. Mempools can be used to efficiently
* allocate data which can then be freed as a whole.
*
* Mempools can also be used to efficiently allocate arbitrarily
* aligned data (such as strings) without incurring the space overhead
* of aligning each allocation (which is not required for strings).
*
* However, each allocation cannot be freed individually, only all
* or nothing.
*
* Return value:
**/
MemPool *e_mempool_new(int blocksize, int threshold, EMemPoolFlags flags)
{
MemPool *pool;
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&mempool_mutex);
#endif
if (mempool_memchunk == NULL) {
mempool_memchunk = e_memchunk_new(8, sizeof(MemPool));
}
pool = e_memchunk_alloc(mempool_memchunk);
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&mempool_mutex);
#endif
if (threshold >= blocksize)
threshold = blocksize * 2 / 3;
pool->blocksize = blocksize;
pool->threshold = threshold;
pool->blocks = NULL;
pool->threshold_blocks = NULL;
switch (flags & E_MEMPOOL_ALIGN_MASK) {
case E_MEMPOOL_ALIGN_STRUCT:
default:
pool->align = G_MEM_ALIGN-1;
break;
case E_MEMPOOL_ALIGN_WORD:
pool->align = 2-1;
break;
case E_MEMPOOL_ALIGN_BYTE:
pool->align = 1-1;
}
return pool;
}
/**
* e_mempool_alloc:
* @pool:
* @size:
*
* Allocate a new data block in the mempool. Size will
* be rounded up to the mempool's alignment restrictions
* before being used.
**/
void *e_mempool_alloc(MemPool *pool, register int size)
{
size = (size + pool->align) & (~(pool->align));
if (size>=pool->threshold) {
MemPoolThresholdNode *n;
n = g_malloc(ALIGNED_SIZEOF(*n) + size);
n->next = pool->threshold_blocks;
pool->threshold_blocks = n;
return (char *) n + ALIGNED_SIZEOF(*n);
} else {
register MemPoolNode *n;
n = pool->blocks;
if (n && n->free >= size) {
n->free -= size;
return (char *) n + ALIGNED_SIZEOF(*n) + n->free;
}
/* maybe we could do some sort of the free blocks based on size, but
it doubt its worth it at all */
n = g_malloc(ALIGNED_SIZEOF(*n) + pool->blocksize);
n->next = pool->blocks;
pool->blocks = n;
n->free = pool->blocksize - size;
return (char *) n + ALIGNED_SIZEOF(*n) + n->free;
}
}
char *e_mempool_strdup(EMemPool *pool, const char *str)
{
char *out;
out = e_mempool_alloc(pool, strlen(str)+1);
strcpy(out, str);
return out;
}
/**
* e_mempool_flush:
* @pool:
* @freeall: Free all system allocated blocks as well.
*
* Flush used memory and mark allocated blocks as free.
*
* If @freeall is #TRUE, then all allocated blocks are free'd
* as well. Otherwise only blocks above the threshold are
* actually freed, and the others are simply marked as empty.
**/
void e_mempool_flush(MemPool *pool, int freeall)
{
MemPoolThresholdNode *tn, *tw;
MemPoolNode *pw, *pn;
tw = pool->threshold_blocks;
while (tw) {
tn = tw->next;
g_free(tw);
tw = tn;
}
pool->threshold_blocks = NULL;
if (freeall) {
pw = pool->blocks;
while (pw) {
pn = pw->next;
g_free(pw);
pw = pn;
}
pool->blocks = NULL;
} else {
pw = pool->blocks;
while (pw) {
pw->free = pool->blocksize;
pw = pw->next;
}
}
}
/**
* e_mempool_destroy:
* @pool:
*
* Free all memory associated with a mempool.
**/
void e_mempool_destroy(MemPool *pool)
{
if (pool) {
e_mempool_flush(pool, 1);
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&mempool_mutex);
#endif
e_memchunk_free(mempool_memchunk, pool);
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&mempool_mutex);
#endif
}
}
/*
string array classes
*/
#define STRV_UNPACKED ((unsigned char)(~0))
struct _EStrv {
unsigned char length; /* how many entries we have (or the token STRV_UNPACKED) */
char data[1]; /* data follows */
};
struct _s_strv_string {
char *string; /* the string to output */
char *free; /* a string to free, if we referenced it */
};
struct _e_strvunpacked {
unsigned char type; /* we overload last to indicate this is unpacked */
MemPool *pool; /* pool of memory for strings */
struct _EStrv *source; /* if we were converted from a packed one, keep the source around for a while */
unsigned int length;
struct _s_strv_string strings[1]; /* the string array data follows */
};
/**
* e_strv_new:
* @size: The number of elements in the strv. Currently this is limited
* to 254 elements.
*
* Create a new strv (string array) header. strv's can be used to
* create and work with arrays of strings that can then be compressed
* into a space-efficient static structure. This is useful
* where a number of strings are to be stored for lookup, and not
* generally edited afterwards.
*
* The size limit is currently 254 elements. This will probably not
* change as arrays of this size suffer significant performance
* penalties when looking up strings with high indices.
*
* Return value:
**/
struct _EStrv *
e_strv_new(int size)
{
struct _e_strvunpacked *s;
g_assert(size<255);
s = g_malloc(sizeof(*s) + (size-1)*sizeof(s->strings[0]));
s(printf("new strv=%p, size = %d bytes\n", s, sizeof(*s) + (size-1)*sizeof(char *)));
s->type = STRV_UNPACKED;
s->pool = NULL;
s->length = size;
s->source = NULL;
memset(s->strings, 0, size*sizeof(s->strings[0]));
return (struct _EStrv *)s;
}
static struct _e_strvunpacked *
strv_unpack(struct _EStrv *strv)
{
struct _e_strvunpacked *s;
register char *p;
int i;
s(printf("unpacking\n"));
s = (struct _e_strvunpacked *)e_strv_new(strv->length);
p = strv->data;
for (i=0;i<s->length;i++) {
if (i>0)
while (*p++)
;
s->strings[i].string = p;
}
s->source = strv;
s->type = STRV_UNPACKED;
return s;
}
/**
* e_strv_set_ref:
* @strv:
* @index:
* @str:
*
* Set a string array element by reference. The string
* is not copied until the array is packed.
*
* If @strv has been packed, then it is unpacked ready
* for more inserts, and should be packed again once finished with.
* The memory used by the original @strv is not freed until
* the new strv is packed, or freed itself.
*
* Return value: A new EStrv if the strv has already
* been packed, otherwise @strv.
**/
struct _EStrv *
e_strv_set_ref(struct _EStrv *strv, int index, char *str)
{
struct _e_strvunpacked *s;
s(printf("set ref %d '%s'\nawkmeharder: %s\n ", index, str, str));
if (strv->length != STRV_UNPACKED)
s = strv_unpack(strv);
else
s = (struct _e_strvunpacked *)strv;
g_assert(index>=0 && index < s->length);
s->strings[index].string = str;
return (struct _EStrv *)s;
}
/**
* e_strv_set_ref_free:
* @strv:
* @index:
* @str:
*
* Set a string by reference, similar to set_ref, but also
* free the string when finished with it. The string
* is not copied until the strv is packed, and not at
* all if the index is overwritten.
*
* Return value: @strv if already unpacked, otherwise an packed
* EStrv.
**/
struct _EStrv *
e_strv_set_ref_free(struct _EStrv *strv, int index, char *str)
{
struct _e_strvunpacked *s;
s(printf("set ref %d '%s'\nawkmeevenharder: %s\n ", index, str, str));
if (strv->length != STRV_UNPACKED)
s = strv_unpack(strv);
else
s = (struct _e_strvunpacked *)strv;
g_assert(index>=0 && index < s->length);
s->strings[index].string = str;
if (s->strings[index].free)
g_free(s->strings[index].free);
s->strings[index].free = str;
return (struct _EStrv *)s;
}
/**
* e_strv_set:
* @strv:
* @index:
* @str:
*
* Set a string array reference. The string @str is copied
* into the string array at location @index.
*
* If @strv has been packed, then it is unpacked ready
* for more inserts, and should be packed again once finished with.
*
* Return value: A new EStrv if the strv has already
* been packed, otherwise @strv.
**/
struct _EStrv *
e_strv_set(struct _EStrv *strv, int index, const char *str)
{
struct _e_strvunpacked *s;
s(printf("set %d '%s'\n", index, str));
if (strv->length != STRV_UNPACKED)
s = strv_unpack(strv);
else
s = (struct _e_strvunpacked *)strv;
g_assert(index>=0 && index < s->length);
if (s->pool == NULL)
s->pool = e_mempool_new(1024, 512, E_MEMPOOL_ALIGN_BYTE);
s->strings[index].string = e_mempool_alloc(s->pool, strlen(str)+1);
strcpy(s->strings[index].string, str);
return (struct _EStrv *)s;
}
/**
* e_strv_pack:
* @strv:
*
* Pack the @strv into a space efficient structure for later lookup.
*
* All strings are packed into a single allocated block, separated
* by single \0 characters, together with a count byte.
*
* Return value:
**/
struct _EStrv *
e_strv_pack(struct _EStrv *strv)
{
struct _e_strvunpacked *s;
int len, i;
register char *src, *dst;
if (strv->length == STRV_UNPACKED) {
s = (struct _e_strvunpacked *)strv;
s(printf("packing string\n"));
len = 0;
for (i=0;i<s->length;i++)
len += s->strings[i].string?strlen(s->strings[i].string)+1:1;
strv = g_malloc(sizeof(*strv) + len);
s(printf("allocating strv=%p, size = %d\n", strv, sizeof(*strv)+len));
strv->length = s->length;
dst = strv->data;
for (i=0;i<s->length;i++) {
if ((src = s->strings[i].string)) {
while ((*dst++ = *src++))
;
} else {
*dst++ = 0;
}
}
e_strv_destroy((struct _EStrv *)s);
}
return strv;
}
/**
* e_strv_get:
* @strv:
* @index:
*
* Retrieve a string by index. This function works
* identically on both packed and unpacked strv's, although
* may be much slower on a packed strv.
*
* Return value:
**/
char *
e_strv_get(struct _EStrv *strv, int index)
{
struct _e_strvunpacked *s;
char *p;
if (strv->length != STRV_UNPACKED) {
g_assert(index>=0 && index < strv->length);
p = strv->data;
while (index > 0) {
while (*p++ != 0)
;
index--;
}
return p;
} else {
s = (struct _e_strvunpacked *)strv;
g_assert(index>=0 && index < s->length);
return s->strings[index].string?s->strings[index].string:"";
}
}
/**
* e_strv_destroy:
* @strv:
*
* Free a strv and all associated memory. Works on packed
* or unpacked strv's.
**/
void
e_strv_destroy(struct _EStrv *strv)
{
struct _e_strvunpacked *s;
int i;
s(printf("freeing strv\n"));
if (strv->length == STRV_UNPACKED) {
s = (struct _e_strvunpacked *)strv;
if (s->pool)
e_mempool_destroy(s->pool);
if (s->source)
e_strv_destroy(s->source);
for (i=0;i<s->length;i++) {
if (s->strings[i].free)
g_free(s->strings[i].free);
}
}
s(printf("freeing strv=%p\n", strv));
g_free(strv);
}
/* string pool stuff */
/* TODO:
garbage collection, using the following technique:
Create a memchunk for each possible size of poolv, and allocate every poolv from those
To garbage collect, scan all memchunk internally, ignoring any free areas (or mark each
poolv when freeing it - set length 0?), and find out which strings are not anywhere,
then free them.
OR:
Just keep a refcount in the hashtable, instead of duplicating the key pointer.
either would also require a free for the mempool, so ignore it for now */
/*#define POOLV_REFCNT*/ /* Define to enable refcounting code that does
automatic garbage collection of unused strings */
static GHashTable *poolv_pool = NULL;
static EMemPool *poolv_mempool = NULL;
#ifdef MALLOC_CHECK
static GPtrArray *poolv_table = NULL;
#endif
#ifdef PROFILE_POOLV
static gulong poolv_hits = 0;
static gulong poolv_misses = 0;
static unsigned long poolv_mem, poolv_count;
#endif
#ifdef G_THREADS_ENABLED
static GStaticMutex poolv_mutex = G_STATIC_MUTEX_INIT;
#endif
struct _EPoolv {
unsigned char length;
char *s[1];
};
/**
* e_poolv_new: @size: The number of elements in the poolv, maximum of 254 elements.
*
* create a new poolv (string vector which shares a global string
* pool). poolv's can be used to work with arrays of strings which
* save memory by eliminating duplicated allocations of the same
* string.
*
* this is useful when you have a log of read-only strings that do not
* go away and are duplicated a lot (such as email headers).
*
* we should probably in the future ref count the strings contained in
* the hash table, but for now let's not.
*
* Return value: new pooled string vector
**/
EPoolv *
e_poolv_new(unsigned int size)
{
EPoolv *poolv;
g_assert(size < 255);
poolv = g_malloc0(sizeof (*poolv) + (size - 1) * sizeof (char *));
poolv->length = size;
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&poolv_mutex);
#endif
if (!poolv_pool)
poolv_pool = g_hash_table_new(g_str_hash, g_str_equal);
if (!poolv_mempool)
poolv_mempool = e_mempool_new(32 * 1024, 512, E_MEMPOOL_ALIGN_BYTE);
#ifdef MALLOC_CHECK
{
int i;
if (poolv_table == NULL)
poolv_table = g_ptr_array_new();
for (i=0;i<poolv_table->len;i++)
MPROBE(poolv_table->pdata[i]);
g_ptr_array_add(poolv_table, poolv);
}
#endif
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&poolv_mutex);
#endif
p(printf("new poolv=%p\tsize=%d\n", poolv, sizeof(*poolv) + (size-1)*sizeof(char *)));
#ifdef PROFILE_POOLV
poolv_count++;
#endif
return poolv;
}
/**
* e_poolv_cpy:
* @dest: destination pooled string vector
* @src: source pooled string vector
*
* Copy the contents of a pooled string vector
*
* Return value: @dest, which may be re-allocated if the strings
* are different lengths.
**/
EPoolv *
e_poolv_cpy(EPoolv *dest, const EPoolv *src)
{
#ifdef POOLV_REFCNT
int i;
unsigned int ref;
char *key;
#endif
p2(g_return_val_if_fail (dest != NULL, NULL));
p2(g_return_val_if_fail (src != NULL, NULL));
MPROBE(dest);
MPROBE(src);
if (dest->length != src->length) {
e_poolv_destroy(dest);
dest = e_poolv_new(src->length);
}
#ifdef POOLV_REFCNT
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&poolv_mutex);
#endif
/* ref new copies */
for (i=0;i<src->length;i++) {
if (src->s[i]) {
if (g_hash_table_lookup_extended(poolv_pool, src->s[i], (void **)&key, (void **)&ref)) {
g_hash_table_insert(poolv_pool, key, (void *)(ref+1));
} else {
g_assert_not_reached();
}
}
}
/* unref the old ones */
for (i=0;i<dest->length;i++) {
if (dest->s[i]) {
if (g_hash_table_lookup_extended(poolv_pool, dest->s[i], (void **)&key, (void **)&ref)) {
/* if ref == 1 free it */
g_assert(ref > 0);
g_hash_table_insert(poolv_pool, key, (void *)(ref-1));
} else {
g_assert_not_reached();
}
}
}
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&poolv_mutex);
#endif
#endif
memcpy(dest->s, src->s, src->length * sizeof (char *));
return dest;
}
#ifdef PROFILE_POOLV
static void
poolv_profile_update (void)
{
static time_t last_time = 0;
time_t new_time;
new_time = time (NULL);
if (new_time - last_time < 5)
return;
printf("poolv profile: %lu hits, %lu misses: %d%% hit rate, memory: %lu, instances: %lu\n",
poolv_hits, poolv_misses,
(int)(100.0 * ((double) poolv_hits / (double) (poolv_hits + poolv_misses))),
poolv_mem, poolv_count);
last_time = new_time;
}
#endif
/**
* e_poolv_set:
* @poolv: pooled string vector
* @index: index in vector of string
* @str: string to set
* @freeit: whether the caller is releasing its reference to the
* string
*
* Set a string vector reference. If the caller will no longer be
* referencing the string, freeit should be TRUE. Otherwise, this
* will duplicate the string if it is not found in the pool.
*
* Return value: @poolv
**/
EPoolv *
e_poolv_set (EPoolv *poolv, int index, char *str, int freeit)
{
#ifdef POOLV_REFCNT
unsigned int ref;
char *key;
#endif
p2(g_return_val_if_fail (poolv != NULL, NULL));
g_assert(index >=0 && index < poolv->length);
MPROBE(poolv);
p(printf("setting %d `%s'\n", index, str));
if (!str) {
#ifdef POOLV_REFCNT
if (poolv->s[index]) {
if (g_hash_table_lookup_extended(poolv_pool, poolv->s[index], (void **)&key, (void **)&ref)) {
g_assert(ref > 0);
g_hash_table_insert(poolv_pool, key, (void *)(ref-1));
} else {
g_assert_not_reached();
}
}
#endif
poolv->s[index] = NULL;
return poolv;
}
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&poolv_mutex);
#endif
#ifdef POOLV_REFCNT
if (g_hash_table_lookup_extended(poolv_pool, str, (void **)&key, (void **)&ref)) {
g_hash_table_insert(poolv_pool, key, (void *)(ref+1));
poolv->s[index] = key;
# ifdef PROFILE_POOLV
poolv_hits++;
poolv_profile_update ();
# endif
} else {
# ifdef PROFILE_POOLV
poolv_misses++;
poolv_mem += strlen(str);
poolv_profile_update ();
# endif
poolv->s[index] = e_mempool_strdup(poolv_mempool, str);
g_hash_table_insert(poolv_pool, poolv->s[index], (void *)1);
}
#else /* !POOLV_REFCNT */
if ((poolv->s[index] = g_hash_table_lookup(poolv_pool, str)) != NULL) {
# ifdef PROFILE_POOLV
poolv_hits++;
poolv_profile_update ();
# endif
} else {
# ifdef PROFILE_POOLV
poolv_misses++;
poolv_mem += strlen(str);
poolv_profile_update ();
# endif
poolv->s[index] = e_mempool_strdup(poolv_mempool, str);
g_hash_table_insert(poolv_pool, poolv->s[index], poolv->s[index]);
}
#endif /* !POOLV_REFCNT */
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&poolv_mutex);
#endif
if (freeit)
g_free(str);
return poolv;
}
/**
* e_poolv_get:
* @poolv: pooled string vector
* @index: index in vector of string
*
* Retrieve a string by index. This could possibly just be a macro.
*
* Since the pool is never freed, this string does not need to be
* duplicated, but should not be modified.
*
* Return value: string at that index.
**/
const char *
e_poolv_get(EPoolv *poolv, int index)
{
g_assert(poolv != NULL);
g_assert(index>= 0 && index < poolv->length);
MPROBE(poolv);
p(printf("get %d = `%s'\n", index, poolv->s[index]));
return poolv->s[index]?poolv->s[index]:"";
}
/**
* e_poolv_destroy:
* @poolv: pooled string vector to free
*
* Free a pooled string vector. This doesn't free the strings from
* the vector, however.
**/
void
e_poolv_destroy(EPoolv *poolv)
{
#ifdef POOLV_REFCNT
int i;
unsigned int ref;
char *key;
MPROBE(poolv);
#ifdef G_THREADS_ENABLED
g_static_mutex_lock(&poolv_mutex);
#endif
#ifdef MALLOC_CHECK
for (i=0;i<poolv_table->len;i++)
MPROBE(poolv_table->pdata[i]);
g_ptr_array_remove_fast(poolv_table, poolv);
#endif
for (i=0;i<poolv->length;i++) {
if (poolv->s[i]) {
if (g_hash_table_lookup_extended(poolv_pool, poolv->s[i], (void **)&key, (void **)&ref)) {
/* if ref == 1 free it */
g_assert(ref > 0);
g_hash_table_insert(poolv_pool, key, (void *)(ref-1));
} else {
g_assert_not_reached();
}
}
}
#ifdef G_THREADS_ENABLED
g_static_mutex_unlock(&poolv_mutex);
#endif
#endif
#ifdef PROFILE_POOLV
poolv_count++;
#endif
g_free(poolv);
}
#if 0
#define CHUNK_SIZE (20)
#define CHUNK_COUNT (32)
#define s(x)
main()
{
int i;
MemChunk *mc;
void *mem, *last;
GMemChunk *gmc;
struct _EStrv *s;
s = strv_new(8);
s = strv_set(s, 1, "Testing 1");
s = strv_set(s, 2, "Testing 2");
s = strv_set(s, 3, "Testing 3");
s = strv_set(s, 4, "Testing 4");
s = strv_set(s, 5, "Testing 5");
s = strv_set(s, 6, "Testing 7");
for (i=0;i<8;i++) {
printf("s[%d] = %s\n", i, strv_get(s, i));
}
s(sleep(5));
printf("packing ...\n");
s = strv_pack(s);
for (i=0;i<8;i++) {
printf("s[%d] = %s\n", i, strv_get(s, i));
}
printf("setting ...\n");
s = strv_set_ref(s, 1, "Testing 1 x");
for (i=0;i<8;i++) {
printf("s[%d] = %s\n", i, strv_get(s, i));
}
printf("packing ...\n");
s = strv_pack(s);
for (i=0;i<8;i++) {
printf("s[%d] = %s\n", i, strv_get(s, i));
}
strv_free(s);
#if 0
time_start("Using memchunks");
mc = memchunk_new(CHUNK_COUNT, CHUNK_SIZE);
for (i=0;i<1000000;i++) {
mem = memchunk_alloc(mc);
if ((i & 1) == 0)
memchunk_free(mc, mem);
}
s(sleep(10));
memchunk_destroy(mc);
time_end("allocating 1000000 memchunks, freeing 500k");
time_start("Using gmemchunks");
gmc = g_mem_chunk_new("memchunk", CHUNK_SIZE, CHUNK_SIZE*CHUNK_COUNT, G_ALLOC_AND_FREE);
for (i=0;i<1000000;i++) {
mem = g_mem_chunk_alloc(gmc);
if ((i & 1) == 0)
g_mem_chunk_free(gmc, mem);
}
s(sleep(10));
g_mem_chunk_destroy(gmc);
time_end("allocating 1000000 gmemchunks, freeing 500k");
time_start("Using memchunks");
mc = memchunk_new(CHUNK_COUNT, CHUNK_SIZE);
for (i=0;i<1000000;i++) {
mem = memchunk_alloc(mc);
}
s(sleep(10));
memchunk_destroy(mc);
time_end("allocating 1000000 memchunks");
time_start("Using gmemchunks");
gmc = g_mem_chunk_new("memchunk", CHUNK_SIZE, CHUNK_COUNT*CHUNK_SIZE, G_ALLOC_ONLY);
for (i=0;i<1000000;i++) {
mem = g_mem_chunk_alloc(gmc);
}
s(sleep(10));
g_mem_chunk_destroy(gmc);
time_end("allocating 1000000 gmemchunks");
time_start("Using malloc");
for (i=0;i<1000000;i++) {
malloc(CHUNK_SIZE);
}
time_end("allocating 1000000 malloc");
#endif
}
#endif
|