Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: chrome/browser/sync/notifier/base/fastalloc.h

Issue 1956001: Moved XMPP notifier library from chrome/browser/sync to chrome/common.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_
6 #define CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_
7
8 #include <assert.h>
9
10 namespace notifier {
11
12 template<class T, size_t Size>
13 class FastAlloc {
14 public:
15 FastAlloc() : buffer_(NULL), size_(0) { }
16 ~FastAlloc() { freeBuffer(); }
17 T* get_buffer(size_t size) {
18 if (size_ != 0) {
19 // We only allow one call to get_buffer. This makes the logic here
20 // simpler, and the user has to worry less about someone else calling
21 // get_buffer again on the same FastAlloc object and invalidating the
22 // memory they were using.
23 assert(false && "get_buffer may one be called once");
24 return NULL;
25 }
26
27 if (size <= Size) {
28 buffer_ = internal_buffer_;
29 } else {
30 buffer_ = new T[size];
31 }
32
33 if (buffer_ != NULL) {
34 size_ = size;
35 }
36 return buffer_;
37 }
38
39 private:
40 void freeBuffer() {
41 #if defined(DEBUG)
42 memset(buffer_, 0xCC, size_ * sizeof(T));
43 #endif
44
45 if (buffer_ != NULL && buffer_ != internal_buffer_) {
46 delete[] buffer_;
47 }
48 buffer_ = NULL;
49 size_ = 0;
50 }
51
52 T* buffer_;
53 T internal_buffer_[Size];
54 size_t size_;
55 };
56
57 }
58
59 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698