File: DAllocator.h

package info (click to toggle)
trafficserver 3.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 17,428 kB
  • sloc: cpp: 222,273; sh: 12,193; ansic: 8,967; makefile: 1,809; perl: 1,158; java: 277; lex: 124; yacc: 63; sed: 6
file content (102 lines) | stat: -rw-r--r-- 2,441 bytes parent folder | download
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
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

/*****************************************************************************

  DAllocator.h

   A pool allocator with some debugging feature including double free
     detection

   The basic idea behind this allocator is that you can store information
     about each allocated block separately from the block allowing easy
     double free detection.  It's also easy to integrate with the
     memory tracker but I found most fast allocated stuff doesn't lend
     it self well to this approach as it's typically allocated through
     wrapper functions


  ****************************************************************************/

#ifndef _D_ALLOCATOR_H_
#define _D_ALLOCATOR_H_

#include "List.h"
#include "ink_mutex.h"

enum DallocState
{
  DALLOC_UNKNOWN = 0,
  DALLOC_FREE,
  DALLOC_IN_USE
};

struct AllocDescriptor
{
  int magic;
  DallocState state;
  void *el;

  LINK(AllocDescriptor, link);      // list of free elements
};

struct DAllocator;
struct AllocPoolDescriptor
{
  AllocPoolDescriptor();
  void add_elements(int num, DAllocator * da);

  void *region_start;
  void *region_end;
  int num_el;
  int el_size;

  AllocDescriptor *descriptors;

  SLINK(AllocPoolDescriptor, link);
};

struct DAllocator
{
  ink_mutex mutex;
  const char *name;
  int alignment;
  int el_size;

  SList(AllocPoolDescriptor,link) pools;
  Que(AllocDescriptor,link) free_list;

  DAllocator();
  ~DAllocator();

  void init(const char *name, unsigned type_size, unsigned alignment);
  void *alloc();
  void free(void *to_free);

private:
  void add_pool(int num_el);
};



#endif