/*********************************************************
- * Copyright (c) 2014-2018 VMware, Inc. All rights reserved.
+ * Copyright (c) 2014-2024 Broadcom. All Rights Reserved.
+ * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
*
*********************************************************/
+#ifndef AUTOCPTR_HH
+#define AUTOCPTR_HH
+
+#include <memory>
+
+
/*
- * autoCPtr.hh --
+ *-----------------------------------------------------------------------------
+ *
+ * auto_unique --
*
- * A simple, std::auto_ptr-like class for managing memory usually
- * allocated by C functions.
+ * A helper function to create and return std::unique_ptr objects with
+ * deduced types.
*
- * Unlike std::auto_ptr, allows providing a customer deleter and disallows
- * copying. This is basically a wanna-be std::unique_ptr for platforms
- * that don't have C++11 available yet.
+ * Returns:
+ * Returns the constructed std::unique_ptr.
*
- * XXX: When everything uses C++11, this can be replaced with
- * std::unique_ptr.
+ * Usage:
+ * auto foo = auto_unique(AllocateFoo(), DeleteFoo);
+ *
+ *-----------------------------------------------------------------------------
*/
-#ifndef AUTOCPTR_HH
-#define AUTOCPTR_HH
-
-#include <cstdlib>
-#include <utility>
-
-
-template<typename T, typename FreeFunc = void (*)(void*)>
-class AutoCPtr
+template<typename T, typename Deleter = std::default_delete<T>>
+std::unique_ptr<T, Deleter>
+auto_unique(T* p, // IN
+ Deleter deleter = Deleter()) // IN/OPT
{
-private:
- typedef AutoCPtr<T, FreeFunc> SelfType;
-
-public:
- explicit AutoCPtr(T* p = NULL, // IN/OPT
- FreeFunc f = std::free) // IN/OPT
- : mP(p),
- mFree(f)
- {
- }
-
- ~AutoCPtr() { mFree(mP); }
-
- void reset(T* p = NULL) // IN/OPT
- {
- if (p == mP) {
- return;
- }
-
- SelfType copy(mP, mFree);
- mP = p;
- }
-
- T* release()
- {
- T* p = mP;
- mP = NULL;
- return p;
- }
-
- T* get() const { return mP; }
- T* operator->() const { return mP; }
- T& operator*() const { return *mP; }
-
- void swap(SelfType& other) // IN/OUT
- {
- using std::swap;
- swap(mP, other.mP);
- swap(mFree, other.mFree);
- }
-
-private:
- T* mP;
- FreeFunc mFree;
-
-private:
- // Non-copyable.
- AutoCPtr(const SelfType&);
- SelfType& operator=(const SelfType&);
-};
+ return {p, deleter};
+}
#endif // AUTOCPTR_HH
/*********************************************************
- * Copyright (c) 2008-2019,2022 VMware, Inc. All rights reserved.
+ * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
+ * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
return;
}
- mUstr = AutoCPtr<char>(
+ mUstr = auto_unique(
Unicode_AllocWithUTF16(static_cast<const utf16_t *>(s)),
free).get();
ASSERT(Validate(mUstr));
*/
mUtf16Cache = Unicode_UTF16Strdup(s);
- mUstr = AutoCPtr<char>(Unicode_AllocWithUTF16(s),
- free).get();
+ mUstr = auto_unique(Unicode_AllocWithUTF16(s), free).get();
ASSERT(Validate(mUstr));
}
return;
}
- mUstr = AutoCPtr<char>(Unicode_Alloc(s, encoding),
- free).get();
+ mUstr = auto_unique(Unicode_Alloc(s, encoding), free).get();
ASSERT(Validate(mUstr));
}
void (*freeFunc)(void*)) // IN/OPT
{
ASSERT(utf8 != NULL);
- return AutoCPtr<char>(utf8, freeFunc).get();
+ return auto_unique(utf8, freeFunc).get();
}
/*********************************************************
- * Copyright (c) 2008-2019,2021-2023 VMware, Inc. All rights reserved.
+ * Copyright (c) 2008-2024 Broadcom. All Rights Reserved.
+ * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
if (s != NULL) {
// Since we already have the UTF-8 version of the string, cache it now.
mUTF8 = std::shared_ptr<UTF8Data>(new UTF8Data(Util_SafeStrdup(s)));
- mBstr = AutoCPtr<utf16_t>(Unicode_GetAllocUTF16(s), free).get();
+ mBstr = auto_unique(Unicode_GetAllocUTF16(s), free).get();
}
}
}
if (mUTF8->Get() == NULL) {
- AutoCPtr<char> utf8Str(
+ auto utf8Str = auto_unique(
Unicode_AllocWithUTF16(static_cast<wchar_t *>(mBstr)),
free);
mUTF8->Set(utf8Str.get());