containers minimum elements, windows memory leaks
This commit is contained in:
34
libs/main/containers/picontainers.cpp
Normal file
34
libs/main/containers/picontainers.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Base macros for generic containers
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "picontainers.h"
|
||||
|
||||
|
||||
const ssize_t minAlloc = 64;
|
||||
|
||||
|
||||
ssize_t _PIContainerConstantsBase::calcMinCountPoT(ssize_t szof) {
|
||||
ssize_t ret = 0, elc = 1;
|
||||
while (elc * szof < minAlloc) {
|
||||
elc *= 2;
|
||||
++ret;
|
||||
}
|
||||
//printf("calcMinCount sizeof = %d, min_count = %d, pot = %d\n", szof, elc, ret);
|
||||
return ret;
|
||||
}
|
||||
@@ -63,6 +63,18 @@ private:
|
||||
};
|
||||
|
||||
|
||||
class PIP_EXPORT _PIContainerConstantsBase {
|
||||
public:
|
||||
static ssize_t calcMinCountPoT(ssize_t szof);
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class _PIContainerConstants {
|
||||
public:
|
||||
static ssize_t minCountPoT() {static ssize_t ret = _PIContainerConstantsBase::calcMinCountPoT(sizeof(T)); return ret;}
|
||||
};
|
||||
|
||||
|
||||
//! \brief
|
||||
//! \~english Template reverse wrapper over any container
|
||||
//! \~russian Шаблонная функция обертки любого контейнера для обратного доступа через итераторы
|
||||
|
||||
@@ -2280,10 +2280,9 @@ private:
|
||||
if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s)) {
|
||||
return pid_rsize + pid_rsize;
|
||||
}
|
||||
ssize_t t = 0, s_ = s - 1;
|
||||
while (s_ >> t) {
|
||||
ssize_t t = _PIContainerConstants<T>::minCountPoT(), s_ = s - 1;
|
||||
while (s_ >> t)
|
||||
++t;
|
||||
}
|
||||
return (1 << t);
|
||||
}
|
||||
template<typename T1 = T, typename std::enable_if<
|
||||
|
||||
@@ -2205,8 +2205,9 @@ private:
|
||||
if (piv_rsize + piv_rsize >= s && piv_rsize < s) {
|
||||
return piv_rsize + piv_rsize;
|
||||
}
|
||||
ssize_t t = 0, s_ = s - 1;
|
||||
while (s_ >> t) ++t;
|
||||
ssize_t t = _PIContainerConstants<T>::minCountPoT(), s_ = s - 1;
|
||||
while (s_ >> t)
|
||||
++t;
|
||||
return (1 << t);
|
||||
}
|
||||
template<typename T1 = T, typename std::enable_if<
|
||||
|
||||
@@ -64,10 +64,16 @@ void errorClear() {
|
||||
|
||||
PIString errorString() {
|
||||
#ifdef WINDOWS
|
||||
char * msg;
|
||||
char * msg = nullptr;
|
||||
int err = GetLastError();
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
|
||||
return "code " + PIString::fromNumber(err) + " - " + PIString(msg);
|
||||
PIString ret = PIStringAscii("code ") + PIString::fromNumber(err) + PIStringAscii(" - ");
|
||||
if (msg) {
|
||||
ret += PIString::fromSystem(msg).trim();
|
||||
LocalFree(msg);
|
||||
} else
|
||||
ret += '?';
|
||||
return ret;
|
||||
#else
|
||||
int e = errno;
|
||||
return PIString("code ") + PIString::fromNumber(e) + " - " + PIString(strerror(e));
|
||||
|
||||
@@ -1001,24 +1001,23 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
ci.index = -1;
|
||||
ci.mtu = 1500;
|
||||
#ifdef WINDOWS
|
||||
PIP_ADAPTER_INFO pAdapterInfo, pAdapter = 0;
|
||||
int ret = 0;
|
||||
ulong ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
||||
pAdapterInfo = (IP_ADAPTER_INFO * ) HeapAlloc(GetProcessHeap(), 0, (sizeof (IP_ADAPTER_INFO)));
|
||||
if (pAdapterInfo == 0) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersinfo";
|
||||
PIP_ADAPTER_INFO pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, sizeof(IP_ADAPTER_INFO));
|
||||
if (!pAdapterInfo) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersInfo";
|
||||
return il;
|
||||
}
|
||||
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
||||
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
||||
pAdapterInfo = (IP_ADAPTER_INFO *) HeapAlloc(GetProcessHeap(), 0, (ulOutBufLen));
|
||||
if (pAdapterInfo == 0) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersinfo";
|
||||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
pAdapterInfo = (PIP_ADAPTER_INFO)HeapAlloc(GetProcessHeap(), 0, ulOutBufLen);
|
||||
if (!pAdapterInfo) {
|
||||
piCout << "[PIEthernet] Error allocating memory needed to call GetAdaptersInfo";
|
||||
return il;
|
||||
}
|
||||
}
|
||||
if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
||||
pAdapter = pAdapterInfo;
|
||||
PIP_ADAPTER_INFO pAdapter = pAdapterInfo;
|
||||
while (pAdapter) {
|
||||
ci.name = PIString(pAdapter->AdapterName);
|
||||
ci.index = pAdapter->Index;
|
||||
@@ -1031,8 +1030,8 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
IP_ADDR_STRING * as = &(pAdapter->IpAddressList);
|
||||
while (as) {
|
||||
// piCout << "[pAdapter]" << ci.name << PIString(as->IpAddress.String);
|
||||
ci.address = PIString(as->IpAddress.String);
|
||||
ci.netmask = PIString(as->IpMask.String);
|
||||
ci.address = PIStringAscii(as->IpAddress.String);
|
||||
ci.netmask = PIStringAscii(as->IpMask.String);
|
||||
if (ci.address == "0.0.0.0") {
|
||||
as = as->Next;
|
||||
continue;
|
||||
@@ -1053,7 +1052,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() {
|
||||
}
|
||||
}
|
||||
if (pAdapterInfo)
|
||||
HeapFree(GetProcessHeap(), 0, (pAdapterInfo));
|
||||
HeapFree(GetProcessHeap(), 0, pAdapterInfo);
|
||||
#else
|
||||
#ifdef MICRO_PIP
|
||||
#else
|
||||
@@ -1213,10 +1212,16 @@ int PIEthernet::ethErrorCore() {
|
||||
|
||||
PIString PIEthernet::ethErrorString() {
|
||||
#ifdef WINDOWS
|
||||
char * msg;
|
||||
char * msg = nullptr;
|
||||
int err = WSAGetLastError();
|
||||
FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
|
||||
return "code " + PIString::fromNumber(err) + " - " + PIString(msg);
|
||||
PIString ret = PIStringAscii("code ") + PIString::fromNumber(err) + PIStringAscii(" - ");
|
||||
if (msg) {
|
||||
ret += PIString::fromSystem(msg).trim();
|
||||
LocalFree(msg);
|
||||
} else
|
||||
ret += '?';
|
||||
return ret;
|
||||
#else
|
||||
return errorString();
|
||||
#endif
|
||||
|
||||
@@ -356,6 +356,7 @@ void PISystemMonitor::run() {
|
||||
|
||||
void PISystemMonitor::gatherThread(llong id) {
|
||||
PISystemMonitor::ThreadStats ts;
|
||||
if (id == 0) return;
|
||||
ts.id = id;
|
||||
#ifdef MICRO_PIP
|
||||
ts.name = tbid.value(id, "<PIThread>");
|
||||
@@ -386,11 +387,12 @@ void PISystemMonitor::gatherThread(llong id) {
|
||||
PISystemTime ct = PISystemTime::current();
|
||||
FILETIME times[4];
|
||||
HANDLE thdl = OpenThread(THREAD_QUERY_INFORMATION, FALSE, DWORD(id));
|
||||
if (thdl == NULL) {
|
||||
if (!thdl) {
|
||||
piCout << "[PISystemMonitor] gatherThread(" << id << "):: OpenThread() error:" << errorString();
|
||||
return;
|
||||
}
|
||||
if (GetThreadTimes(thdl, &(times[0]), &(times[1]), &(times[2]), &(times[3])) == 0) {
|
||||
CloseHandle(thdl);
|
||||
piCout << "[PISystemMonitor] gatherThread(" << id << "):: GetThreadTimes() error:" << errorString();
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user