File: pl-bag.c

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (287 lines) | stat: -rw-r--r-- 7,203 bytes parent folder | download | duplicates (2)
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
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2012, University of Amsterdam
			      VU University Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/*#define O_DEBUG 1*/
#include "pl-incl.h"

#undef LD
#define LD LOCAL_LD

		 /*******************************
		 *	    TEMP MALLOC		*
		 *******************************/

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Allocate memory for  findall  bags  in   chunks  that  can  be discarded
together  and  preallocate  the  first    chunk.  This  approach  avoids
fragmentation and reduces the number of  allocation calls. The latter is
notably needed to reduce allocation contention   due to intensive use of
findall/3.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

#define FIRST_CHUNK_SIZE (64*sizeof(void*))

typedef struct mem_chunk
{ struct mem_chunk *prev;
  size_t	size;
  size_t	used;
} mem_chunk;

typedef struct mem_pool
{ mem_chunk    *chunks;
  mem_chunk	first;
  char		first_data[FIRST_CHUNK_SIZE];
} mem_pool;

static void
init_mem_pool(mem_pool *mp)
{ mp->chunks = &mp->first;
  mp->first.size = FIRST_CHUNK_SIZE;
  mp->first.used = 0;
}

#define ROUNDUP(n,m) (((n) + (m - 1)) & ~(m-1))

static void *
alloc_mem_pool(mem_pool *mp, size_t bytes)
{ char *ptr;

  if ( mp->chunks->used + bytes <= mp->chunks->size )
  { ptr = &((char *)(mp->chunks+1))[mp->chunks->used];
    mp->chunks->used += ROUNDUP(bytes, sizeof(void*));
  } else
  { size_t chunksize = (bytes < 1000 ? 4000 : bytes);
    mem_chunk *c = PL_malloc_atomic_unmanaged(chunksize+sizeof(mem_chunk));

    if ( c )
    { c->size    = chunksize;
      c->used    = ROUNDUP(bytes, sizeof(void*));
      c->prev    = mp->chunks;
      mp->chunks = c;
      ptr        = (char *)(mp->chunks+1);
    } else
      return NULL;
  }

#ifdef O_DEBUG
  assert((uintptr_t)ptr%sizeof(void*) == 0);
#endif

  return ptr;
}

static void
clear_mem_pool(mem_pool *mp)
{ mem_chunk *c, *p;

  for(c=mp->chunks; c != &mp->first; c=p)
  { p = c->prev;
    PL_free(c);
  }
}


		 /*******************************
		 *        FINDALL SUPPORT	*
		 *******************************/

#define FINDALL_MAGIC	0x37ac78fe

typedef struct findall_bag
{ struct findall_bag *parent;		/* parent bag */
  intptr_t	magic;			/* FINDALL_MAGIC */
  size_t	solutions;		/* count # solutions */
  size_t	gsize;			/* required size on stack */
  mem_pool	records;		/* stored records */
  segstack	answers;		/* list of answers */
  Record	answer_buf[64];		/* tmp space */
} findall_bag;


static
PRED_IMPL("$new_findall_bag", 0, new_findall_bag, 0)
{ PRED_LD
  findall_bag *bag;

  if ( !LD->bags.bags )			/* outer one */
  { if ( !LD->bags.default_bag )
    {
#ifdef O_ATOMGC
      simpleMutexInit(&LD->bags.mutex);
#endif
      LD->bags.default_bag = PL_malloc(sizeof(*bag));
    }
    bag = LD->bags.default_bag;
  } else
  { bag = PL_malloc(sizeof(*bag));
  }

  if ( !bag )
    return PL_no_memory();

  bag->magic     = FINDALL_MAGIC;
  bag->solutions = 0;
  bag->gsize     = 0;
  bag->parent    = LD->bags.bags;
  init_mem_pool(&bag->records);
  initSegStack(&bag->answers, sizeof(Record),
	       sizeof(bag->answer_buf), bag->answer_buf);
  MemoryBarrier();
  LD->bags.bags = bag;

  return TRUE;
}


static void *
alloc_record(void *ctx, size_t bytes)
{ findall_bag *bag = ctx;

  return alloc_mem_pool(&bag->records, bytes);
}


static
PRED_IMPL("$add_findall_bag", 1, add_findall_bag, 0)
{ PRED_LD
  findall_bag *bag = LD->bags.bags;
  Record r;

  if ( !(r = compileTermToHeap__LD(A1, alloc_record, bag, R_NOLOCK PASS_LD)) )
    return PL_no_memory();
  if ( !pushRecordSegStack(&bag->answers, r) )
    return PL_no_memory();
  bag->gsize += r->gsize;
  bag->solutions++;

  if ( bag->gsize + bag->solutions*3 > limitStack(global)/sizeof(word) )
    return outOfStack(&LD->stacks.global, STACK_OVERFLOW_RAISE);

  return FALSE;				/* force backtracking of generator */
}


static
PRED_IMPL("$collect_findall_bag", 2, collect_findall_bag, 0)
{ PRED_LD
  findall_bag *bag = LD->bags.bags;

  if ( bag->solutions )
  { size_t space = bag->gsize + bag->solutions*3;
    term_t list = PL_copy_term_ref(A2);
    term_t answer = PL_new_term_ref();
    Record *rp;
    int rc;

    if ( !hasGlobalSpace(space) )
    { if ( (rc=ensureGlobalSpace(space, ALLOW_GC)) != TRUE )
	return raiseStackOverflow(rc);
    }

    while ( (rp=topOfSegStack(&bag->answers)) )
    { Record r = *rp;
      copyRecordToGlobal(answer, r, ALLOW_GC PASS_LD);
      if (GD->atoms.gc_active)
        markAtomsRecord(r);
      PL_cons_list(list, answer, list);
#ifdef O_ATOMGC
		/* see comment with scanSegStack() for synchronization details */
      if ( !quickPopTopOfSegStack(&bag->answers) )
      { simpleMutexLock(&LD->bags.mutex);
	popTopOfSegStack(&bag->answers);
	simpleMutexUnlock(&LD->bags.mutex);
      }
#else
      popTopOfSegStack(&bag->answers);
#endif
    }
    DEBUG(CHK_SECURE, assert(emptySegStack(&bag->answers)));

    return PL_unify(A1, list);
  } else
    return PL_unify(A1, A2);
}


static
PRED_IMPL("$destroy_findall_bag", 0, destroy_findall_bag, 0)
{ PRED_LD
  findall_bag *bag = LD->bags.bags;

  assert(bag);
  assert(bag->magic == FINDALL_MAGIC);

#ifdef O_ATOMGC
  simpleMutexLock(&LD->bags.mutex);
#endif
  LD->bags.bags = bag->parent;
#ifdef O_ATOMGC
  simpleMutexUnlock(&LD->bags.mutex);
#endif

  bag->magic = 0;
  clearSegStack(&bag->answers);
  clear_mem_pool(&bag->records);
  if ( bag != LD->bags.default_bag )
    PL_free(bag);

  return TRUE;
}


		 /*******************************
		 *	  ATOM-GC SUPPORT	*
		 *******************************/

static void
markAtomsAnswers(void *data)
{ Record r = *((Record*)data);

  markAtomsRecord(r);
}


void
markAtomsFindall(PL_local_data_t *ld)
{ findall_bag *bag;

  if ( ld->bags.default_bag )
  { simpleMutexLock(&ld->bags.mutex);
    bag = ld->bags.bags;
    for( ; bag; bag = bag->parent )
      scanSegStack(&bag->answers, markAtomsAnswers);
    simpleMutexUnlock(&ld->bags.mutex);
  }
}


		 /*******************************
		 *      PUBLISH PREDICATES	*
		 *******************************/

BeginPredDefs(bag)
  PRED_DEF("$new_findall_bag", 0, new_findall_bag, 0)
  PRED_DEF("$add_findall_bag", 1, add_findall_bag, 0)
  PRED_DEF("$collect_findall_bag", 2, collect_findall_bag, 0)
  PRED_DEF("$destroy_findall_bag", 0, destroy_findall_bag, 0)
EndPredDefs