blob: baf42ffe6a1fa48e1b7e5938e0f2a0bca58f5004 [file] [log] [blame]
Feng Xiao6ef984a2014-11-10 17:34:54 -08001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// https://developers.google.com/protocol-buffers/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
8//
9// * Redistributions of source code must retain the above copyright
10// notice, this list of conditions and the following disclaimer.
11// * Redistributions in binary form must reproduce the above
12// copyright notice, this list of conditions and the following disclaimer
13// in the documentation and/or other materials provided with the
14// distribution.
15// * Neither the name of Google Inc. nor the names of its
16// contributors may be used to endorse or promote products derived from
17// this software without specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// This file exists for testing allocation behavior when using arenas by hooking
32// new/delete. It is a copy of //experimental/mvels/util/new_delete_capture.cc.
33
34#include <google/protobuf/new_delete_capture.h>
35
36#include <pthread.h>
37
38#include <google/protobuf/stubs/common.h>
39#include <google/protobuf/stubs/malloc_hook.h>
40#include <google/protobuf/stubs/spinlock.h>
41
42namespace google {
43namespace {
44
45pthread_t gthread;
46protobuf_unittest::NewDeleteCapture *ghooked_instance = NULL;
47SpinLock gspinlock(base::LINKER_INITIALIZED);
48
49} // namespace
50
51namespace protobuf_unittest {
52
53NewDeleteCapture::NewDeleteCapture()
54 : alloc_count_(0),
55 alloc_size_(0),
56 alloc_ptr_(NULL),
57 free_count_(0),
58 free_ptr_(NULL) {}
59
60NewDeleteCapture::~NewDeleteCapture() { Unhook(); }
61
62void NewDeleteCapture::Reset() {
63 alloc_count_ = 0;
64 alloc_size_ = 0;
65 free_count_ = 0;
66 alloc_ptr_ = NULL;
67 free_ptr_ = NULL;
68}
69
70bool NewDeleteCapture::Hook(bool reset) {
71 SpinLockHolder spinlock(&gspinlock);
72 if (ghooked_instance != this) {
73 GOOGLE_CHECK(ghooked_instance == NULL)
74 << " NewDeleteCapture can have only 1 active instance";
75 GOOGLE_CHECK(MallocHook::AddNewHook(NewHook));
76 GOOGLE_CHECK(MallocHook::AddDeleteHook(DeleteHook));
77 gthread = pthread_self();
78 ghooked_instance = this;
79 if (reset) {
80 Reset();
81 }
82 return true;
83 }
84 return false;
85}
86
87bool NewDeleteCapture::Unhook() {
88 SpinLockHolder spinlock(&gspinlock);
89 if (ghooked_instance == this) {
90 gthread = pthread_t();
91 ghooked_instance = NULL;
92 GOOGLE_CHECK(MallocHook::RemoveDeleteHook(DeleteHook));
93 GOOGLE_CHECK(MallocHook::RemoveNewHook(NewHook));
94 return true;
95 }
96 return false;
97}
98
99void NewDeleteCapture::NewHook(const void *ptr, size_t size) {
100 SpinLockHolder spinlock(&gspinlock);
101 if (gthread == pthread_self()) {
102 auto &rthis = *ghooked_instance;
103 if (++rthis.alloc_count_ == 1) {
104 rthis.alloc_size_ = size;
105 rthis.alloc_ptr_ = ptr;
106 }
107 }
108}
109
110void NewDeleteCapture::DeleteHook(const void *ptr) {
111 SpinLockHolder spinlock(&gspinlock);
112 if (gthread == pthread_self()) {
113 auto &rthis = *ghooked_instance;
114 if (++rthis.free_count_ == 1) {
115 rthis.free_ptr_ = ptr;
116 }
117 }
118}
119
120} // namespace protobuf_unittest
121} // namespace google