/** * Assignment operator. Performs a deep copy of list into this list. * * @param list The list to copy * * @return A reference to this list */ template <class T> inline Vector<T>& Vector<T>::operator=(const Vector<T>& list) { // clear the list clear(); // increase the capacity to hold the new list if (capacity < list.count) { capacity = 0; delete [] elems; elems = new T*[list.capacity]; if (!elems) throw OutOfMemoryException(); capacity = list.capacity; } // copy elements for (size_t i = 0; i < list.count; i++) { elems[i] = new T( *(list.elems[i]) ); if (!elems[i]) throw OutOfMemoryException(); count++; } return *this; }
void* MemoryManagerImpl::allocate(XMLSize_t size) { void* memptr; try { memptr = ::operator new(size); } catch(...) { throw OutOfMemoryException(); } if(memptr==NULL && size!=0) throw OutOfMemoryException(); return memptr; }
void SerialChannelImpl::handleError(const std::string& name) { std::string errorText; DWORD error = GetLastError(); switch (error) { case ERROR_FILE_NOT_FOUND: throw FileNotFoundException(name, getErrorText(errorText)); case ERROR_ACCESS_DENIED: throw FileAccessDeniedException(name, getErrorText(errorText)); case ERROR_ALREADY_EXISTS: case ERROR_FILE_EXISTS: throw FileExistsException(name, getErrorText(errorText)); case ERROR_FILE_READ_ONLY: throw FileReadOnlyException(name, getErrorText(errorText)); case ERROR_CANNOT_MAKE: case ERROR_INVALID_NAME: case ERROR_FILENAME_EXCED_RANGE: throw CreateFileException(name, getErrorText(errorText)); case ERROR_BROKEN_PIPE: case ERROR_INVALID_USER_BUFFER: case ERROR_INSUFFICIENT_BUFFER: throw IOException(name, getErrorText(errorText)); case ERROR_NOT_ENOUGH_MEMORY: throw OutOfMemoryException(name, getErrorText(errorText)); case ERROR_HANDLE_EOF: break; default: throw FileException(name, getErrorText(errorText)); } }
void SQLite::exec(const String &query) { #ifndef HAVE_SQLITE3 throw UnsupportedFeatureException("SQLite"); #else if (!conn) throw NoConnectionException(); String err; double t_start; affectedrows=0; sqlite3_stmt *stmt=NULL; t_start=GetMicrotime(); int ret=sqlite3_prepare_v2((sqlite3*)conn, (const char*)query, query.size(),&stmt,NULL); if (ret!=SQLITE_OK) { throw QueryFailedException("sqlite3_prepare_v2 failed: %s",sqlite3_errmsg((sqlite3*)conn)); } if (stmt==NULL) { throw OutOfMemoryException(); } ret=sqlite3_step(stmt); if (ret!=SQLITE_DONE && ret!=SQLITE_ROW) { err.setf("sqlite3_step: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); sqlite3_finalize(stmt); throw QueryFailedException(err); } ret=sqlite3_finalize(stmt); if (ret !=SQLITE_OK) { err.setf("sqlite3_finalize: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); throw QueryFailedException(err); } affectedrows=sqlite3_changes((sqlite3*)conn); updateLastUse(); logQuery(query,(float)(GetMicrotime()-t_start)); #endif }
XERCES_CPP_NAMESPACE_BEGIN void* MemoryManagerImpl::allocate(size_t size) { void* memptr; try { memptr = ::operator new(size); } catch(...) { throw OutOfMemoryException(); } if (memptr != NULL) { return memptr; } throw OutOfMemoryException(); }
POCO_LPQUERY_SERVICE_CONFIG WinService::config() const { open(); int size = 4096; DWORD bytesNeeded; POCO_LPQUERY_SERVICE_CONFIG pSvcConfig = (POCO_LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, size); if (!pSvcConfig) throw OutOfMemoryException("cannot allocate service config buffer"); try { #if defined(POCO_WIN32_UTF8) while (!QueryServiceConfigW(_svcHandle, pSvcConfig, size, &bytesNeeded)) #else while (!QueryServiceConfigA(_svcHandle, pSvcConfig, size, &bytesNeeded)) #endif { if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { LocalFree(pSvcConfig); size = bytesNeeded; pSvcConfig = (POCO_LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, size); } else throw SystemException("cannot query service configuration", _name); } } catch (...) { LocalFree(pSvcConfig); throw; } return pSvcConfig; }
/** * Default constructor that accepts an optional capacity argument. * * @param capacity The initial capacity of the vector. */ template <class T> inline Vector<T>::Vector(int capacity) : elems(0), count(0), capacity(0) { elems = new T*[capacity]; if (!elems) throw OutOfMemoryException(); this->capacity = capacity; }
/** * Increases the capacity of the vector */ template <class T> inline void Vector<T>::grow() { // pick a new capacity size_t newSize = capacity; if (newSize < 16) newSize = 16; newSize <<= 2; if (newSize <= capacity) throw IntegerWrapException(); // allocate the memory T** newElems = new T*[newSize]; if (!newElems) throw OutOfMemoryException(); // copy everything over if (elems) { memcpy(newElems, elems, count * sizeof(T*)); // get rid of the old array delete [] elems; } // put it all in place elems = newElems; capacity = newSize; }
/** * Add data to be sent. (The data will be copied into the send buffers and it is allowed to modify it after this call.) */ void CBufferedWriter::Enqueue(const uint8_t* buffer, const uint32_t size) { m_totalLength += size; uint32_t remainingSize = size; //Do we need another buffer ? If yes, flush everything that's possible and allocate a new one. while(m_KernelBufRemainingBytes < remainingSize) { if(0 < m_KernelBufRemainingBytes) { memcpy(m_KernelBufCursor, buffer, m_KernelBufRemainingBytes); buffer += m_KernelBufRemainingBytes; remainingSize -= m_KernelBufRemainingBytes; } bool bSuccess = m_pSendableBufferProvider->GetNextBuffer(m_KernelBufCursor, m_KernelBufRemainingBytes); if (false == bSuccess) { printf("Error: BufferedWriter was unable to obtain more transmission buffers.\n"); throw OutOfMemoryException(); } } //Flush the rest (or everything if the above while loop wasn't entered) into the current buffer memcpy(m_KernelBufCursor, buffer, remainingSize); m_KernelBufCursor += remainingSize; m_KernelBufRemainingBytes -= remainingSize; }
aol::Vector<_DataType>::Vector ( const qc::GridStructure &Grid ) : _size ( Grid.getNumberOfNodes() ), _sizeReserved ( _size ), _deleteFlag ( true ), overflowHandling ( aol::CLIP ), overflowMin ( 0 ), overflowMax ( aol::OverflowTrait<DataType>::max ) { _pData = static_cast<DataType*> ( aol::MemoryManager::allocateAtLeast ( _sizeReserved, sizeof ( DataType ) ) ); if ( !_pData ) throw OutOfMemoryException ( "Vector<DataType,Realtype>::Vector: Could not allocate memory for Vector.", __FILE__, __LINE__ ); setZero (); }
/** * Copy constructor. Performs a deep copy of list. * * @param list The vector to copy */ template <class T> inline Vector<T>::Vector(const Vector<T>& list) : elems(0), count(0), capacity(0) { // allocate memory elems = new T*[list.capacity]; if (!elems) throw OutOfMemoryException(); capacity = list.capacity; // copy elements for (size_t i = 0; i < list.count; i++) { elems[i] = new T( *(list.elems[i]) ); if (!elems[i]) throw OutOfMemoryException(); count++; } }
void* safe_malloc(size_t size) { void *ptr = std::malloc(size); if (ptr == NULL) { throw OutOfMemoryException(size); } return ptr; }
void SocketMessage::setPayload(const String &msg) { clear(); payload_type=Variant::TYPE_STRING; payload_size=msg.size(); payload=strndup(msg.c_str(),payload_size); if (!payload) throw OutOfMemoryException(); }
void SocketMessage::setPayload(const AssocArray &msg) { clear(); payload_type=Variant::TYPE_ASSOCARRAY; payload_size=msg.binarySize(); payload=malloc(payload_size); if (!payload) throw OutOfMemoryException(); msg.exportBinary(payload,payload_size,NULL); }
/** Constructor. * @param name_format format of name to identify the listener, * see sprintf for supported tokens */ BlackBoardInterfaceListener::BlackBoardInterfaceListener(const char *name_format, ...) { va_list arg; va_start(arg, name_format); if (vasprintf(&__name, name_format, arg) == -1) { throw OutOfMemoryException("BlackBoardInterfaceListener ctor: vasprintf() failed"); } va_end(arg); }
void SocketMessage::setPayload(const ByteArrayPtr &msg) { clear(); payload_type=Variant::TYPE_BYTEARRAY; payload_size=msg.size(); payload=malloc(payload_size); if (!payload) throw OutOfMemoryException(); memcpy(payload,msg.ptr(),payload_size); }
XERCES_CPP_NAMESPACE_BEGIN void* MemoryManagerArrayImpl::allocate(size_t size) { void* memptr; try { //return ::operator new[](size); //return new char[size]; memptr = new char[size]; } catch(...) { throw OutOfMemoryException(); } if (memptr != NULL) { return memptr; } throw OutOfMemoryException(); }
void* safe_realloc(void *var, size_t size) { void *ptr = NULL; ptr = std::realloc(var, size); if (ptr == NULL && size != 0) { throw OutOfMemoryException(size); } return ptr; }
ResultSet *SQLite::query(const String &query) { #ifndef HAVE_SQLITE3 throw UnsupportedFeatureException("SQLite"); #else if (!conn) throw NoConnectionException(); String err; double t_start; affectedrows=0; sqlite3_stmt *stmt=NULL; t_start=GetMicrotime(); int ret=sqlite3_prepare_v2((sqlite3*)conn, (const char*)query, query.size(),&stmt,NULL); if (ret!=SQLITE_OK) { throw QueryFailedException("sqlite3_prepare_v2 failed: %s",sqlite3_errmsg((sqlite3*)conn)); } if (stmt==NULL) { throw OutOfMemoryException(); } ret=sqlite3_step(stmt); if (ret!=SQLITE_DONE && ret!=SQLITE_ROW) { err.setf("sqlite3_step: %s, Query: %s",sqlite3_errmsg((sqlite3*)conn),(const char*)query); sqlite3_finalize(stmt); throw QueryFailedException(err); } affectedrows=sqlite3_changes((sqlite3*)conn); updateLastUse(); logQuery(query,(float)(GetMicrotime()-t_start)); SQLiteResult *pr=new SQLiteResult; if (!pr) { sqlite3_finalize(stmt); throw OutOfMemoryException(); } pr->stmt=stmt; pr->last_res=ret; pr->sqlite_class=this; pr->conn=(sqlite3 *)conn; pr->affectedrows=affectedrows; pr->num_fields=sqlite3_column_count(stmt); return pr; #endif }
/** Execute file on a specific Lua state. * @param L Lua state to execute the file in. * @param filename filet to load and excute. */ void LuaContext::do_file(lua_State *L, const char *filename) { // Load initialization code int err = 0; std::string errmsg; if ( (err = luaL_loadfile(L, filename)) != 0) { errmsg = lua_tostring(L, -1); lua_pop(L, 1); switch (err) { case LUA_ERRSYNTAX: throw SyntaxErrorException("Lua syntax error in file %s: %s", filename, errmsg.c_str()); case LUA_ERRMEM: throw OutOfMemoryException("Could not load Lua file %s", filename); case LUA_ERRFILE: throw CouldNotOpenFileException(filename, errmsg.c_str()); } } int errfunc = __enable_tracebacks ? 1 : 0; if ( (err = lua_pcall(L, 0, LUA_MULTRET, errfunc)) != 0 ) { // There was an error while executing the initialization file errmsg = lua_tostring(L, -1); lua_pop(L, 1); switch (err) { case LUA_ERRRUN: throw LuaRuntimeException("do_file", errmsg.c_str()); case LUA_ERRMEM: throw OutOfMemoryException("Could not execute Lua file %s", filename); case LUA_ERRERR: throw LuaErrorException("do_file", errmsg.c_str()); default: throw LuaErrorException("do_file/unknown error", errmsg.c_str()); } } }
const char * RRDGraphGPrint::to_string() const { if (! __string) { if (asprintf(&__string, "GPRINT:%s:%s:%s", __def_name, RRDArchive::cf_to_string(__cf), __format) == -1) { throw OutOfMemoryException("Failed to create RRD graph GPRINT string"); } } return __string; }
/** Set thread instance in thread-specific data (TSD). * Use thread-specific data to store a reference to the Thread instance in the * pthread struct. Used by current_thread(). * @param t thread to set specific data on */ void Thread::set_tsd_thread_instance(Thread *t) { int err = 0; if ( (err = pthread_setspecific(THREAD_KEY, t)) != 0 ) { if ( ENOMEM == err ) { throw OutOfMemoryException("Could not set specific data (reference to thread)"); } else { throw Exception("Could not set specific data (reference to thread), unknown reason"); } } }
/** Create string representation. * @return string representation suitable for rrd_graph_v(). */ const char * RRDGraphDataDefinition::to_string() const { if (! __string) { if (__rpn_expression) { if (asprintf(&__string, "CDEF:%s=%s", __name, __rpn_expression) == -1) { throw OutOfMemoryException("Failed to create RRA string"); } } else { size_t ds_index = __rrd_def->find_ds_index(__ds_name); if (asprintf(&__string, "DEF:%s=%s:%s:%s", __name, __rrd_def->get_filename(), __rrd_def->get_ds(ds_index).get_name(), RRDArchive::cf_to_string(__cf)) == -1) { throw OutOfMemoryException("Failed to create RRA string"); } } } return __string; }
/** Convert int value to a string. * @param i value to convert * @return string representation of value. */ std::string StringConversions::to_string(const int i) { char *tmp; std::string rv; if (asprintf(&tmp, "%i", i) == -1) { throw OutOfMemoryException("StringConversions::tostring(const int): asprintf() failed"); } rv = tmp; free(tmp); return rv; }
const char * RRDGraphArea::to_string() const { if (! __string) { if (asprintf(&__string, "AREA:%s#%s:%s%s", __def_name, __color, __legend, __stacked ? ":STACK" : "") == -1) { throw OutOfMemoryException("Failed to create RRD graph AREA string"); } } return __string; }
/** * Add a copy of item to the end of the list. * * @param item The item to add. */ template <class T> inline void Vector<T>::append(const T& item) { // ensure capacity if (count >= capacity) grow(); // create the item we'll hold on to T* t = new T(item); if (!t) throw OutOfMemoryException(); // put it in the list elems[count++] = t; }
/** Set name of thread. * If you want a more descriptive thread name you can do so by calling this method * in your thread's constructor, and only in the constructor. * Use parameters similar to printf(). * @param format format string */ void Thread::set_name(const char *format, ...) { va_list va; va_start(va, format); char *old_name = __name; if (vasprintf(&__name, format, va) == -1) { __name = old_name; throw OutOfMemoryException("Could not set new thread name for '%s'", __name); } else { free(old_name); } va_end(va); }
void ESolver::CheckResourceLimits() { if(ResourceLimitManager::CheckTimeOut()) { EndSolve(); SolveEndTime = TimeValue::GetTimeValue(); SolveEndMemStats = MemStats::GetMemStats(); throw OutOfTimeException("Query time limit exceeded"); } else if(ResourceLimitManager::CheckMemOut()) { EndSolve(); SolveEndTime = TimeValue::GetTimeValue(); SolveEndMemStats = MemStats::GetMemStats(); throw OutOfMemoryException("Out of memory"); } }
/** Get string reprensetation. * @return string representation suitable to be bassed to rrd_create(). */ const char * RRDDataSource::to_string() const { if (! __string) { if (__type == COMPUTE) { if (asprintf(&__string, "DS:%s:COMPUTE:%s", __name, __rpn_expression) == -1) { throw OutOfMemoryException("Failed to create DS string for %s", __name); } } else { const char *type_string; switch (__type) { case COUNTER: type_string = "COUNTER"; break; case DERIVE: type_string = "DERIVE"; break; case ABSOLUTE: type_string = "ABSOLUTE"; break; default: type_string = "GAUGE"; break; } char min_s[32]; char max_s[32]; if (__min == UNKNOWN) { strcpy(min_s, "U"); } else { snprintf(min_s, 32, "%f", __min); } if (__max == UNKNOWN) { strcpy(max_s, "U"); } else { snprintf(max_s, 32, "%f", __max); } if (asprintf(&__string, "DS:%s:%s:%u:%s:%s", __name, type_string, __heartbeat, min_s, max_s) == -1) { throw OutOfMemoryException("Failed to create DS string for %s", __name); } } } return __string; }
/** Set name of thread. * Use parameters similar to printf(). * @param format format string */ void ThreadList::set_name(const char *format, ...) { va_list va; va_start(va, format); char *tmpname; if (vasprintf(&tmpname, format, va) != -1) { free(__name); __name = tmpname; } else { throw OutOfMemoryException("ThreadList::set_name(): vasprintf() failed"); } va_end(va); }