~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to lib/ts/ink_queue.cc

  • Committer: Package Import Robot
  • Author(s): Aron Xu
  • Date: 2013-05-09 01:00:04 UTC
  • mto: (1.1.11) (5.3.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 15.
  • Revision ID: package-import@ubuntu.com-20130509010004-9fqq9n0adseg3f8w
Tags: upstream-3.3.2
ImportĀ upstreamĀ versionĀ 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
#include <assert.h>
41
41
#include <memory.h>
42
42
#include <stdlib.h>
 
43
#include <unistd.h>
43
44
#include <sys/types.h>
44
45
#include <sys/mman.h>
45
46
#include "ink_atomic.h"
48
49
#include "ink_error.h"
49
50
#include "ink_assert.h"
50
51
#include "ink_resource.h"
51
 
 
52
 
 
53
 
typedef struct _ink_freelist_list
54
 
{
55
 
  InkFreeList *fl;
56
 
  struct _ink_freelist_list *next;
57
 
}
58
 
ink_freelist_list;
 
52
#include "ink_queue_ext.h"
 
53
 
59
54
 
60
55
inkcoreapi volatile int64_t fastalloc_mem_in_use = 0;
61
56
inkcoreapi volatile int64_t fastalloc_mem_total = 0;
79
74
static const int page_size = 8192;   /* sysconf (_SC_PAGESIZE); */
80
75
#endif
81
76
 
82
 
static ink_freelist_list *freelists = NULL;
 
77
ink_freelist_list *freelists = NULL;
83
78
 
84
79
inkcoreapi volatile int64_t freelist_allocated_mem = 0;
85
80
 
86
81
#define fl_memadd(_x_) \
87
82
   ink_atomic_increment(&freelist_allocated_mem, (int64_t) (_x_));
88
83
 
89
 
 
90
84
void
91
 
ink_freelist_init(InkFreeList * f,
92
 
                  const char *name, uint32_t type_size, uint32_t chunk_size, uint32_t offset, uint32_t alignment)
 
85
ink_freelist_init(InkFreeList **fl, const char *name, uint32_t type_size,
 
86
                  uint32_t chunk_size, uint32_t alignment)
93
87
{
 
88
#if TS_USE_RECLAIMABLE_FREELIST
 
89
  return reclaimable_freelist_init(fl, name, type_size, chunk_size, alignment);
 
90
#else
 
91
  InkFreeList *f;
94
92
  ink_freelist_list *fll;
95
93
 
96
94
  /* its safe to add to this global list because ink_freelist_init()
97
95
     is only called from single-threaded initialization code. */
 
96
  f = (InkFreeList *)ats_memalign(alignment, sizeof(InkFreeList));
98
97
  fll = (ink_freelist_list *)ats_malloc(sizeof(ink_freelist_list));
99
98
  fll->fl = f;
100
99
  fll->next = freelists;
101
100
  freelists = fll;
102
101
 
103
102
  f->name = name;
104
 
  f->offset = offset;
105
103
  /* quick test for power of 2 */
106
104
  ink_assert(!(alignment & (alignment - 1)));
107
105
  f->alignment = alignment;
113
111
  f->allocated = 0;
114
112
  f->allocated_base = 0;
115
113
  f->count_base = 0;
 
114
  *fl = f;
 
115
#endif
116
116
}
117
117
 
118
118
InkFreeList *
119
 
ink_freelist_create(const char *name, uint32_t type_size, uint32_t chunk_size, uint32_t offset, uint32_t alignment)
 
119
ink_freelist_create(const char *name, uint32_t type_size, uint32_t chunk_size,
 
120
                    uint32_t alignment)
120
121
{
121
 
  InkFreeList *f = (InkFreeList *)ats_malloc(sizeof(InkFreeList));
 
122
  InkFreeList *f;
122
123
 
123
 
  ink_freelist_init(f, name, type_size, chunk_size, offset, alignment);
 
124
  ink_freelist_init(&f, name, type_size, chunk_size, alignment);
124
125
  return f;
125
126
}
126
127
 
131
132
#endif
132
133
 
133
134
int fastmemtotal = 0;
134
 
 
135
135
void *
136
136
ink_freelist_new(InkFreeList * f)
137
137
{
138
138
#if TS_USE_FREELIST
 
139
#if TS_USE_RECLAIMABLE_FREELIST
 
140
  return reclaimable_freelist_new(f);
 
141
#else
139
142
  head_p item;
140
143
  head_p next;
141
144
  int result = 0;
142
145
 
143
146
  do {
144
 
    INK_QUEUE_LD64(item, f->head);
 
147
    INK_QUEUE_LD(item, f->head);
145
148
    if (TO_PTR(FREELIST_POINTER(item)) == NULL) {
146
149
      uint32_t type_size = f->type_size;
147
150
      uint32_t i;
196
199
      ink_atomic_increment(&fastalloc_mem_in_use, (int64_t) f->chunk_size * f->type_size);
197
200
 
198
201
    } else {
199
 
      SET_FREELIST_POINTER_VERSION(next, *ADDRESS_OF_NEXT(TO_PTR(FREELIST_POINTER(item)), f->offset),
 
202
      SET_FREELIST_POINTER_VERSION(next, *ADDRESS_OF_NEXT(TO_PTR(FREELIST_POINTER(item)), 0),
200
203
                                   FREELIST_VERSION(item) + 1);
201
 
      result = ink_atomic_cas((int64_t *) & f->head.data, item.data, next.data);
 
204
#if TS_HAS_128BIT_CAS
 
205
       result = ink_atomic_cas((__int128_t*)&f->head.data, item.data, next.data);
 
206
#else
 
207
       result = ink_atomic_cas((int64_t *) & f->head.data, item.data, next.data);
 
208
#endif
202
209
 
203
210
#ifdef SANITY
204
211
      if (result) {
220
227
  ink_atomic_increment(&fastalloc_mem_in_use, (int64_t) f->type_size);
221
228
 
222
229
  return TO_PTR(FREELIST_POINTER(item));
 
230
#endif /* TS_USE_RECLAIMABLE_FREELIST */
223
231
#else // ! TS_USE_FREELIST
224
232
  void *newp = NULL;
225
233
 
236
244
ink_freelist_free(InkFreeList * f, void *item)
237
245
{
238
246
#if TS_USE_FREELIST
239
 
  volatile_void_p *adr_of_next = (volatile_void_p *) ADDRESS_OF_NEXT(item, f->offset);
 
247
#if TS_USE_RECLAIMABLE_FREELIST
 
248
  return reclaimable_freelist_free(f, item);
 
249
#else
 
250
  volatile_void_p *adr_of_next = (volatile_void_p *) ADDRESS_OF_NEXT(item, 0);
240
251
  head_p h;
241
252
  head_p item_pair;
242
253
  int result;
255
266
 
256
267
  result = 0;
257
268
  do {
258
 
    INK_QUEUE_LD64(h, f->head);
 
269
    INK_QUEUE_LD(h, f->head);
259
270
#ifdef SANITY
260
271
    if (TO_PTR(FREELIST_POINTER(h)) == item)
261
272
      ink_fatal(1, "ink_freelist_free: trying to free item twice");
267
278
    *adr_of_next = FREELIST_POINTER(h);
268
279
    SET_FREELIST_POINTER_VERSION(item_pair, FROM_PTR(item), FREELIST_VERSION(h));
269
280
    INK_MEMORY_BARRIER;
270
 
    result = ink_atomic_cas((int64_t *) & f->head, h.data, item_pair.data);
 
281
#if TS_HAS_128BIT_CAS
 
282
       result = ink_atomic_cas((__int128_t*) & f->head, h.data, item_pair.data);
 
283
#else
 
284
       result = ink_atomic_cas((int64_t *) & f->head, h.data, item_pair.data);
 
285
#endif
 
286
 
271
287
  }
272
288
  while (result == 0);
273
289
 
274
290
  ink_atomic_increment((int *) &f->count, -1);
275
291
  ink_atomic_increment(&fastalloc_mem_in_use, -(int64_t) f->type_size);
 
292
#endif /* TS_USE_RECLAIMABLE_FREELIST */
276
293
#else
277
294
  if (f->alignment)
278
295
    ats_memalign_free(item);
372
389
  head_p next;
373
390
  int result = 0;
374
391
  do {
375
 
    INK_QUEUE_LD64(item, l->head);
 
392
    INK_QUEUE_LD(item, l->head);
376
393
    if (TO_PTR(FREELIST_POINTER(item)) == NULL)
377
394
      return NULL;
378
395
    SET_FREELIST_POINTER_VERSION(next, *ADDRESS_OF_NEXT(TO_PTR(FREELIST_POINTER(item)), l->offset),
379
396
                                 FREELIST_VERSION(item) + 1);
380
397
#if !defined(INK_USE_MUTEX_FOR_ATOMICLISTS)
381
 
    result = ink_atomic_cas((int64_t *) & l->head.data, item.data, next.data);
 
398
#if TS_HAS_128BIT_CAS
 
399
       result = ink_atomic_cas((__int128_t*) & l->head.data, item.data, next.data);
 
400
#else
 
401
       result = ink_atomic_cas((int64_t *) & l->head.data, item.data, next.data);
 
402
#endif
382
403
#else
383
404
    l->head.data = next.data;
384
405
    result = 1;
405
426
  head_p next;
406
427
  int result = 0;
407
428
  do {
408
 
    INK_QUEUE_LD64(item, l->head);
 
429
    INK_QUEUE_LD(item, l->head);
409
430
    if (TO_PTR(FREELIST_POINTER(item)) == NULL)
410
431
      return NULL;
411
432
    SET_FREELIST_POINTER_VERSION(next, FROM_PTR(NULL), FREELIST_VERSION(item) + 1);
412
433
#if !defined(INK_USE_MUTEX_FOR_ATOMICLISTS)
413
 
    result = ink_atomic_cas((int64_t *) & l->head.data, item.data, next.data);
 
434
#if TS_HAS_128BIT_CAS
 
435
       result = ink_atomic_cas((__int128_t*) & l->head.data, item.data, next.data);
 
436
#else
 
437
       result = ink_atomic_cas((int64_t *) & l->head.data, item.data, next.data);
 
438
#endif
414
439
#else
415
440
    l->head.data = next.data;
416
441
    result = 1;
444
469
  head_p item_pair;
445
470
  int result = 0;
446
471
  volatile void *h = NULL;
447
 
  ink_assert(*adr_of_next == NULL);
448
472
  do {
449
 
    INK_QUEUE_LD64(head, l->head);
 
473
    INK_QUEUE_LD(head, l->head);
450
474
    h = FREELIST_POINTER(head);
451
475
    *adr_of_next = h;
452
476
    ink_assert(item != TO_PTR(h));
453
477
    SET_FREELIST_POINTER_VERSION(item_pair, FROM_PTR(item), FREELIST_VERSION(head));
454
478
    INK_MEMORY_BARRIER;
455
479
#if !defined(INK_USE_MUTEX_FOR_ATOMICLISTS)
456
 
    result = ink_atomic_cas((int64_t *) & l->head, head.data, item_pair.data);
 
480
#if TS_HAS_128BIT_CAS
 
481
       result = ink_atomic_cas((__int128_t*) & l->head, head.data, item_pair.data);
 
482
#else
 
483
       result = ink_atomic_cas((int64_t *) & l->head, head.data, item_pair.data);
 
484
#endif
457
485
#else
458
486
    l->head.data = item_pair.data;
459
487
    result = 1;
482
510
  /*
483
511
   * first, try to pop it if it is first
484
512
   */
485
 
  INK_QUEUE_LD64(head, l->head);
 
513
  INK_QUEUE_LD(head, l->head);
486
514
  while (TO_PTR(FREELIST_POINTER(head)) == item) {
487
515
    head_p next;
488
516
    SET_FREELIST_POINTER_VERSION(next, item_next, FREELIST_VERSION(head) + 1);
489
517
#if !defined(INK_USE_MUTEX_FOR_ATOMICLISTS)
490
 
    result = ink_atomic_cas((int64_t *) & l->head.data, head.data, next.data);
 
518
#if TS_HAS_128BIT_CAS
 
519
       result = ink_atomic_cas((__int128_t*) & l->head.data, head.data, next.data);
 
520
#else
 
521
       result = ink_atomic_cas((int64_t *) & l->head.data, head.data, next.data);
 
522
#endif
491
523
#else
492
524
    l->head.data = next.data;
493
525
    result = 1;
496
528
      *addr_next = NULL;
497
529
      return item;
498
530
    }
499
 
    INK_QUEUE_LD64(head, l->head);
 
531
    INK_QUEUE_LD(head, l->head);
500
532
  }
501
533
 
502
534
  /*