@@ -170,6 +170,7 @@ Format of STDIN stream:
170170#include "run-command.h"
171171#include "packfile.h"
172172#include "object-store.h"
173+ #include "mem-pool.h"
173174
174175#define PACK_ID_BITS 16
175176#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -211,13 +212,6 @@ struct last_object {
211212 unsigned no_swap : 1 ;
212213};
213214
214- struct mem_pool {
215- struct mem_pool * next_pool ;
216- char * next_free ;
217- char * end ;
218- uintmax_t space [FLEX_ARRAY ]; /* more */
219- };
220-
221215struct atom_str {
222216 struct atom_str * next_atom ;
223217 unsigned short str_len ;
@@ -306,9 +300,8 @@ static int global_argc;
306300static const char * * global_argv ;
307301
308302/* Memory pools */
309- static size_t mem_pool_alloc = 2 * 1024 * 1024 - sizeof (struct mem_pool );
310- static size_t total_allocd ;
311- static struct mem_pool * mem_pool ;
303+ static struct mem_pool fi_mem_pool = {NULL , 2 * 1024 * 1024 -
304+ sizeof (struct mp_block ), 0 };
312305
313306/* Atom management */
314307static unsigned int atom_table_sz = 4451 ;
@@ -343,6 +336,7 @@ static unsigned int tree_entry_alloc = 1000;
343336static void * avail_tree_entry ;
344337static unsigned int avail_tree_table_sz = 100 ;
345338static struct avail_tree_content * * avail_tree_table ;
339+ static size_t tree_entry_allocd ;
346340static struct strbuf old_tree = STRBUF_INIT ;
347341static struct strbuf new_tree = STRBUF_INIT ;
348342
@@ -636,49 +630,10 @@ static unsigned int hc_str(const char *s, size_t len)
636630 return r ;
637631}
638632
639- static void * pool_alloc (size_t len )
640- {
641- struct mem_pool * p ;
642- void * r ;
643-
644- /* round up to a 'uintmax_t' alignment */
645- if (len & (sizeof (uintmax_t ) - 1 ))
646- len += sizeof (uintmax_t ) - (len & (sizeof (uintmax_t ) - 1 ));
647-
648- for (p = mem_pool ; p ; p = p -> next_pool )
649- if ((p -> end - p -> next_free >= len ))
650- break ;
651-
652- if (!p ) {
653- if (len >= (mem_pool_alloc /2 )) {
654- total_allocd += len ;
655- return xmalloc (len );
656- }
657- total_allocd += sizeof (struct mem_pool ) + mem_pool_alloc ;
658- p = xmalloc (st_add (sizeof (struct mem_pool ), mem_pool_alloc ));
659- p -> next_pool = mem_pool ;
660- p -> next_free = (char * ) p -> space ;
661- p -> end = p -> next_free + mem_pool_alloc ;
662- mem_pool = p ;
663- }
664-
665- r = p -> next_free ;
666- p -> next_free += len ;
667- return r ;
668- }
669-
670- static void * pool_calloc (size_t count , size_t size )
671- {
672- size_t len = count * size ;
673- void * r = pool_alloc (len );
674- memset (r , 0 , len );
675- return r ;
676- }
677-
678633static char * pool_strdup (const char * s )
679634{
680635 size_t len = strlen (s ) + 1 ;
681- char * r = pool_alloc ( len );
636+ char * r = mem_pool_alloc ( & fi_mem_pool , len );
682637 memcpy (r , s , len );
683638 return r ;
684639}
@@ -687,7 +642,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
687642{
688643 struct mark_set * s = marks ;
689644 while ((idnum >> s -> shift ) >= 1024 ) {
690- s = pool_calloc ( 1 , sizeof (struct mark_set ));
645+ s = mem_pool_calloc ( & fi_mem_pool , 1 , sizeof (struct mark_set ));
691646 s -> shift = marks -> shift + 10 ;
692647 s -> data .sets [0 ] = marks ;
693648 marks = s ;
@@ -696,7 +651,7 @@ static void insert_mark(uintmax_t idnum, struct object_entry *oe)
696651 uintmax_t i = idnum >> s -> shift ;
697652 idnum -= i << s -> shift ;
698653 if (!s -> data .sets [i ]) {
699- s -> data .sets [i ] = pool_calloc ( 1 , sizeof (struct mark_set ));
654+ s -> data .sets [i ] = mem_pool_calloc ( & fi_mem_pool , 1 , sizeof (struct mark_set ));
700655 s -> data .sets [i ]-> shift = s -> shift - 10 ;
701656 }
702657 s = s -> data .sets [i ];
@@ -734,7 +689,7 @@ static struct atom_str *to_atom(const char *s, unsigned short len)
734689 if (c -> str_len == len && !strncmp (s , c -> str_dat , len ))
735690 return c ;
736691
737- c = pool_alloc ( sizeof (struct atom_str ) + len + 1 );
692+ c = mem_pool_alloc ( & fi_mem_pool , sizeof (struct atom_str ) + len + 1 );
738693 c -> str_len = len ;
739694 memcpy (c -> str_dat , s , len );
740695 c -> str_dat [len ] = 0 ;
@@ -765,7 +720,7 @@ static struct branch *new_branch(const char *name)
765720 if (check_refname_format (name , REFNAME_ALLOW_ONELEVEL ))
766721 die ("Branch name doesn't conform to GIT standards: %s" , name );
767722
768- b = pool_calloc ( 1 , sizeof (struct branch ));
723+ b = mem_pool_calloc ( & fi_mem_pool , 1 , sizeof (struct branch ));
769724 b -> name = pool_strdup (name );
770725 b -> table_next_branch = branch_table [hc ];
771726 b -> branch_tree .versions [0 ].mode = S_IFDIR ;
@@ -801,7 +756,7 @@ static struct tree_content *new_tree_content(unsigned int cnt)
801756 avail_tree_table [hc ] = f -> next_avail ;
802757 } else {
803758 cnt = cnt & 7 ? ((cnt / 8 ) + 1 ) * 8 : cnt ;
804- f = pool_alloc ( sizeof (* t ) + sizeof (t -> entries [0 ]) * cnt );
759+ f = mem_pool_alloc ( & fi_mem_pool , sizeof (* t ) + sizeof (t -> entries [0 ]) * cnt );
805760 f -> entry_capacity = cnt ;
806761 }
807762
@@ -846,7 +801,7 @@ static struct tree_entry *new_tree_entry(void)
846801
847802 if (!avail_tree_entry ) {
848803 unsigned int n = tree_entry_alloc ;
849- total_allocd += n * sizeof (struct tree_entry );
804+ tree_entry_allocd += n * sizeof (struct tree_entry );
850805 ALLOC_ARRAY (e , n );
851806 avail_tree_entry = e ;
852807 while (n -- > 1 ) {
@@ -2867,7 +2822,7 @@ static void parse_new_tag(const char *arg)
28672822 enum object_type type ;
28682823 const char * v ;
28692824
2870- t = pool_alloc ( sizeof (struct tag ));
2825+ t = mem_pool_alloc ( & fi_mem_pool , sizeof (struct tag ));
28712826 memset (t , 0 , sizeof (struct tag ));
28722827 t -> name = pool_strdup (arg );
28732828 if (last_tag )
@@ -3466,12 +3421,12 @@ int cmd_main(int argc, const char **argv)
34663421 atom_table = xcalloc (atom_table_sz , sizeof (struct atom_str * ));
34673422 branch_table = xcalloc (branch_table_sz , sizeof (struct branch * ));
34683423 avail_tree_table = xcalloc (avail_tree_table_sz , sizeof (struct avail_tree_content * ));
3469- marks = pool_calloc ( 1 , sizeof (struct mark_set ));
3424+ marks = mem_pool_calloc ( & fi_mem_pool , 1 , sizeof (struct mark_set ));
34703425
34713426 global_argc = argc ;
34723427 global_argv = argv ;
34733428
3474- rc_free = pool_alloc ( cmd_save * sizeof (* rc_free ));
3429+ rc_free = mem_pool_alloc ( & fi_mem_pool , cmd_save * sizeof (* rc_free ));
34753430 for (i = 0 ; i < (cmd_save - 1 ); i ++ )
34763431 rc_free [i ].next = & rc_free [i + 1 ];
34773432 rc_free [cmd_save - 1 ].next = NULL ;
@@ -3545,8 +3500,8 @@ int cmd_main(int argc, const char **argv)
35453500 fprintf (stderr , "Total branches: %10lu (%10lu loads )\n" , branch_count , branch_load_count );
35463501 fprintf (stderr , " marks: %10" PRIuMAX " (%10" PRIuMAX " unique )\n" , (((uintmax_t )1 ) << marks -> shift ) * 1024 , marks_set_count );
35473502 fprintf (stderr , " atoms: %10u\n" , atom_cnt );
3548- fprintf (stderr , "Memory total: %10" PRIuMAX " KiB\n" , (total_allocd + alloc_count * sizeof (struct object_entry ))/1024 );
3549- fprintf (stderr , " pools: %10lu KiB\n" , (unsigned long )(total_allocd /1024 ));
3503+ fprintf (stderr , "Memory total: %10" PRIuMAX " KiB\n" , (tree_entry_allocd + fi_mem_pool . pool_alloc + alloc_count * sizeof (struct object_entry ))/1024 );
3504+ fprintf (stderr , " pools: %10lu KiB\n" , (unsigned long )(( tree_entry_allocd + fi_mem_pool . pool_alloc ) /1024 ));
35503505 fprintf (stderr , " objects: %10" PRIuMAX " KiB\n" , (alloc_count * sizeof (struct object_entry ))/1024 );
35513506 fprintf (stderr , "---------------------------------------------------------------------\n" );
35523507 pack_report ();
0 commit comments