Fix refcounting in arReturn() and stop leaking static strings.
[hiphop-php.git] / hphp / runtime / vm / pc-filter.h
blobc3ee1b17b8e6b49fc3b254cdca0f2837102949c0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 Facebook, Inc. (http://www.facebook.com) |
6 | Copyright (c) 1997-2010 The PHP Group |
7 +----------------------------------------------------------------------+
8 | This source file is subject to version 3.01 of the PHP license, |
9 | that is bundled with this package in the file LICENSE, and is |
10 | available through the world-wide-web at the following url: |
11 | http://www.php.net/license/3_01.txt |
12 | If you did not receive a copy of the PHP license and are unable to |
13 | obtain it through the world-wide-web, please send a note to |
14 | license@php.net so we can mail you a copy immediately. |
15 +----------------------------------------------------------------------+
18 #ifndef incl_HPHP_PC_FILTER_H_
19 #define incl_HPHP_PC_FILTER_H_
21 #include "hphp/runtime/vm/unit.h"
23 namespace HPHP {
24 ////////////////////////////////////////////////////////////////////////////////
26 // Map which holds a set of PCs and supports reasonably fast addition and
27 // lookup. Used to decide if a given PC falls within an interesting area, e.g.,
28 // for breakpoints and stepping.
29 class PCFilter {
30 private:
31 // Radix-tree implementation of pointer map
32 struct PtrMapNode;
33 class PtrMap {
34 #define PTRMAP_PTR_SIZE (sizeof(void*) * 8)
35 #define PTRMAP_LEVEL_BITS 8LL
36 #define PTRMAP_LEVEL_ENTRIES (1LL << PTRMAP_LEVEL_BITS)
37 #define PTRMAP_LEVEL_MASK (PTRMAP_LEVEL_ENTRIES - 1LL)
39 public:
40 PtrMap() {
41 static_assert(PTRMAP_PTR_SIZE % PTRMAP_LEVEL_BITS == 0,
42 "PTRMAP_PTR_SIZE must be a multiple of PTRMAP_LEVEL_BITS");
43 m_root = MakeNode();
45 ~PtrMap();
46 void setPointer(void* ptr, void* val);
47 void* getPointer(void* ptr);
48 void clear();
50 private:
51 PtrMapNode* m_root;
52 static PtrMapNode* MakeNode();
55 PtrMap m_map;
57 public:
58 PCFilter() {}
60 // Filter function to exclude opcodes when adding ranges.
61 typedef std::function<bool(Op)> OpcodeFilter;
63 // Add/remove offsets, either individually or by range. By default allow all
64 // opcodes.
65 void addRanges(const Unit* unit, const OffsetRangeVec& offsets,
66 OpcodeFilter isOpcodeAllowed = [] (Op) { return true; });
67 void removeRanges(const Unit* unit, const OffsetRangeVec& offsets,
68 OpcodeFilter isOpcodeAllowed = [] (Op) { return true; });
69 void removeOffset(const Unit* unit, Offset offset);
71 // Add/remove/check explicit PCs.
72 void addPC(const unsigned char* pc) {
73 m_map.setPointer((void*)pc, (void*)pc);
75 void removePC(const unsigned char* pc) {
76 m_map.setPointer((void*)pc, nullptr);
78 bool checkPC(const unsigned char* pc) {
79 return m_map.getPointer((void*)pc) == (void*)pc;
82 void clear() {
83 m_map.clear();
87 ///////////////////////////////////////////////////////////////////////////////
90 #endif // incl_HPHP_PC_FILTER_H_