cbdata.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#ifndef SQUID_SRC_CBDATA_H
10#define SQUID_SRC_CBDATA_H
11
195typedef int cbdata_type;
197
204
218void *cbdataInternalFree(void *p);
219
220void cbdataInternalLock(const void *p);
221void cbdataInternalUnlock(const void *p);
222
238int cbdataInternalReferenceDoneValid(void **p, void **tp);
239#define cbdataReferenceValidDone(var, ptr) cbdataInternalReferenceDoneValid((void **)&(var), (ptr))
240
247int cbdataReferenceValid(const void *p);
248
254cbdata_type cbdataInternalAddType(cbdata_type type, const char *label, int size);
255
257#define CBDATA_DECL_(type, methodSpecifiers) \
258 public: \
259 void *operator new(size_t size) { \
260 assert(size == sizeof(type)); \
261 if (!CBDATA_##type) CBDATA_##type = cbdataInternalAddType(CBDATA_##type, #type, sizeof(type)); \
262 return (type *)cbdataInternalAlloc(CBDATA_##type); \
263 } \
264 void operator delete (void *address) { \
265 if (address) cbdataInternalFree(address); \
266 } \
267 void *toCbdata() methodSpecifiers { return this; } \
268 private: \
269 static cbdata_type CBDATA_##type;
270
276{
277public:
278 virtual ~CbdataParent() {}
279 virtual void *toCbdata() = 0;
280
281private:
283 virtual void finalizedInCbdataChild() = 0;
284};
285
289#define CBDATA_CLASS(type) CBDATA_DECL_(type, noexcept)
290
294#define CBDATA_CHILD(type) CBDATA_DECL_(type, final) \
295 void finalizedInCbdataChild() final {}
296
306#define CBDATA_INTERMEDIATE() \
307 public: \
308 void *toCbdata() override { return this; } \
309 private:
310
320#define CBDATA_CLASS_INIT(type) cbdata_type type::CBDATA_##type = CBDATA_UNKNOWN
321
328#define CBDATA_NAMESPACED_CLASS_INIT(namespace, type) cbdata_type namespace::type::CBDATA_##type = CBDATA_UNKNOWN
329
343#define cbdataReference(var) (cbdataInternalLock(var), var)
344
352#define cbdataReferenceDone(var) do {if (var) {cbdataInternalUnlock(var); var = nullptr;}} while(0)
353
361{
363
364public:
365 generic_cbdata(void *aData) : data(aData) {}
366
367 template<typename wrapped_type>void unwrap(wrapped_type **output) {
368 *output = static_cast<wrapped_type *>(data);
369 delete this;
370 }
371
372private:
373 void *data;
374};
375
376// Discouraged: Use CbcPointer<> and asynchronous calls instead if possible.
379{
380public:
381 CallbackData(): data_(nullptr) {}
382 CallbackData(void *data): data_(cbdataReference(data)) {}
384 CallbackData(CallbackData &&other): data_(other.data_) { other.data_ = nullptr; }
386
387 CallbackData &operator =(const CallbackData &other);
388 CallbackData &operator =(CallbackData &&other) { cbdataReferenceDone(data_); data_ = other.data_; other.data_ = nullptr; return *this; }
389
390 bool valid() const { return cbdataReferenceValid(data_); }
391 void *validDone() { void *result; return cbdataReferenceValidDone(data_, &result) ? result : nullptr; }
392
393private:
394 void *data_;
395};
396
397#endif /* SQUID_CBDATA_H */
398
int size
Definition: ModDevPoll.cc:75
void * cbdataInternalFree(void *p)
Definition: cbdata.cc:203
void cbdataInternalUnlock(const void *p)
Definition: cbdata.cc:238
#define cbdataReferenceDone(var)
Definition: cbdata.h:352
#define cbdataReference(var)
Definition: cbdata.h:343
static const cbdata_type CBDATA_UNKNOWN
Definition: cbdata.h:196
int cbdataInternalReferenceDoneValid(void **p, void **tp)
Definition: cbdata.cc:282
#define cbdataReferenceValidDone(var, ptr)
Definition: cbdata.h:239
cbdata_type cbdataInternalAddType(cbdata_type type, const char *label, int size)
Definition: cbdata.cc:117
int cbdataReferenceValid(const void *p)
Definition: cbdata.cc:265
void * cbdataInternalAlloc(cbdata_type type)
Definition: cbdata.cc:145
#define CBDATA_CLASS(type)
Definition: cbdata.h:289
void cbdataInternalLock(const void *p)
Definition: cbdata.cc:221
int cbdata_type
Definition: cbdata.h:195
an old-style void* callback parameter
Definition: cbdata.h:379
~CallbackData()
Definition: cbdata.h:385
void * validDone()
Definition: cbdata.h:391
bool valid() const
Definition: cbdata.h:390
CallbackData(CallbackData &&other)
Definition: cbdata.h:384
CallbackData()
Definition: cbdata.h:381
CallbackData(void *data)
Definition: cbdata.h:382
CallbackData & operator=(const CallbackData &other)
Definition: cbdata.cc:300
void * data_
raw callback data, maybe invalid
Definition: cbdata.h:394
CallbackData(const CallbackData &other)
Definition: cbdata.h:383
virtual void finalizedInCbdataChild()=0
hack: ensure CBDATA_CHILD() after a toCbdata()-defining CBDATA_INTERMEDIATE()
virtual void * toCbdata()=0
virtual ~CbdataParent()
Definition: cbdata.h:278
void * data
Definition: cbdata.h:373
void unwrap(wrapped_type **output)
Definition: cbdata.h:367
generic_cbdata(void *aData)
Definition: cbdata.h:365

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors