2018-05-04 17:55:46 -07:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2018 FIRST. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#ifndef WPIUTIL_WPI_MEMORY_H_
|
|
|
|
|
#define WPIUTIL_WPI_MEMORY_H_
|
2018-05-04 17:55:46 -07:00
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <exception>
|
|
|
|
|
|
|
|
|
|
#include "wpi/raw_ostream.h"
|
|
|
|
|
|
|
|
|
|
namespace wpi {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around std::calloc that calls std::terminate on out of memory.
|
|
|
|
|
* @param num number of objects to allocate
|
|
|
|
|
* @param size number of bytes per object to allocate
|
|
|
|
|
* @return Pointer to beginning of newly allocated memory.
|
|
|
|
|
*/
|
|
|
|
|
inline void* CheckedCalloc(size_t num, size_t size) {
|
|
|
|
|
void* p = std::calloc(num, size);
|
|
|
|
|
if (!p) {
|
|
|
|
|
errs() << "FATAL: failed to allocate " << (num * size) << " bytes\n";
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around std::malloc that calls std::terminate on out of memory.
|
|
|
|
|
* @param size number of bytes to allocate
|
|
|
|
|
* @return Pointer to beginning of newly allocated memory.
|
|
|
|
|
*/
|
|
|
|
|
inline void* CheckedMalloc(size_t size) {
|
|
|
|
|
void* p = std::malloc(size == 0 ? 1 : size);
|
|
|
|
|
if (!p) {
|
|
|
|
|
errs() << "FATAL: failed to allocate " << size << " bytes\n";
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Wrapper around std::realloc that calls std::terminate on out of memory.
|
|
|
|
|
* @param ptr memory previously allocated
|
|
|
|
|
* @param size number of bytes to allocate
|
|
|
|
|
* @return Pointer to beginning of newly allocated memory.
|
|
|
|
|
*/
|
|
|
|
|
inline void* CheckedRealloc(void* ptr, size_t size) {
|
|
|
|
|
void* p = std::realloc(ptr, size == 0 ? 1 : size);
|
|
|
|
|
if (!p) {
|
|
|
|
|
errs() << "FATAL: failed to allocate " << size << " bytes\n";
|
|
|
|
|
std::terminate();
|
|
|
|
|
}
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace wpi
|
|
|
|
|
|
2018-05-13 17:09:56 -07:00
|
|
|
#endif // WPIUTIL_WPI_MEMORY_H_
|