summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Volinsky <mataes2007@gmail.com>2015-05-20 09:05:09 +0000
committerKirill Volinsky <mataes2007@gmail.com>2015-05-20 09:05:09 +0000
commit33401f05553ebe71d59fc3f271fbd6d7609529ee (patch)
tree40845b2693f59b7f5e7015084503266a326c88ba
parent40d79a3a7a8b6ea2036ce7a945429d0e6871430b (diff)
memory.cpp synced with mir_core memory.cpp
git-svn-id: http://svn.miranda-ng.org/main/trunk@13708 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
-rw-r--r--src/core/memory.cpp134
1 files changed, 69 insertions, 65 deletions
diff --git a/src/core/memory.cpp b/src/core/memory.cpp
index 8585c7e71b..890ff84647 100644
--- a/src/core/memory.cpp
+++ b/src/core/memory.cpp
@@ -68,21 +68,21 @@ void* mir_alloc(size_t size)
{
if (size == 0)
return NULL;
- {
- char* p = (char*)malloc(size + sizeof(DWORD)*3);
- if (p == NULL) {
- OutputDebugStringA("memory overflow\n");
- #if defined(_DEBUG)
- DebugBreak();
- #endif
- return NULL;
- }
- *(DWORD*)p = (DWORD)size;
- *(DWORD*)&p[ sizeof(DWORD) ] = BLOCK_ALLOCED;
- *(DWORD*)&p[ size + sizeof(DWORD)*2 ] = BLOCK_ALLOCED;
- return p + sizeof(DWORD)*2;
-} }
+ char *p = (char*)malloc(size + sizeof(DWORD)* 3);
+ if (p == NULL) {
+ OutputDebugStringA("memory overflow\n");
+ #if defined(_DEBUG)
+ DebugBreak();
+ #endif
+ return NULL;
+ }
+
+ *(DWORD*)p = (DWORD)size;
+ *(DWORD*)&p[sizeof(DWORD)] = BLOCK_ALLOCED;
+ *(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_ALLOCED;
+ return p + sizeof(DWORD)* 2;
+}
/******************************************************************************/
@@ -98,10 +98,10 @@ void* mir_calloc(size_t size)
void* mir_realloc(void* ptr, size_t size)
{
- char* p;
+ char *p;
if (ptr != NULL) {
- if ( !CheckBlock(ptr))
+ if (!CheckBlock(ptr))
return NULL;
p = (char*)ptr - sizeof(DWORD)*2;
}
@@ -117,8 +117,8 @@ void* mir_realloc(void* ptr, size_t size)
}
*(DWORD*)p = (DWORD)size;
- *(DWORD*)&p[ sizeof(DWORD) ] = BLOCK_ALLOCED;
- *(DWORD*)&p[ size + sizeof(DWORD)*2 ] = BLOCK_ALLOCED;
+ *(DWORD*)&p[sizeof(DWORD)] = BLOCK_ALLOCED;
+ *(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_ALLOCED;
return p + sizeof(DWORD)*2;
}
@@ -131,55 +131,67 @@ void mir_free(void* ptr)
if (ptr == NULL)
return;
- if ( !CheckBlock(ptr))
+ if (!CheckBlock(ptr))
return;
p = (char*)ptr - sizeof(DWORD)*2;
size = *(DWORD*)p;
- *(DWORD*)&p[ sizeof(DWORD) ] = BLOCK_FREED;
- *(DWORD*)&p[ size + sizeof(DWORD)*2 ] = BLOCK_FREED;
+
+ *(DWORD*)&p[sizeof(DWORD)] = BLOCK_FREED;
+ *(DWORD*)&p[size + sizeof(DWORD)*2] = BLOCK_FREED;
free(p);
}
/******************************************************************************/
-char* mir_strdup(const char* str)
+char* mir_strdup(const char *str)
{
- if (str != NULL) {
- char* p = (char*)mir_alloc(strlen(str)+1);
- if (p)
- strcpy(p, str);
- return p;
- }
- return NULL;
+ if (str == NULL)
+ return NULL;
+
+ char *p = (char*)mir_alloc(strlen(str)+1);
+ if (p)
+ strcpy(p, str);
+ return p;
+}
+
+WCHAR* mir_wstrdup(const WCHAR *str)
+{
+ if (str == NULL)
+ return NULL;
+
+ WCHAR *p = (WCHAR*)mir_alloc(sizeof(WCHAR)*(wcslen(str)+1));
+ if (p)
+ wcscpy(p, str);
+ return p;
}
/******************************************************************************/
-char* mir_strndup(const char* str, size_t len)
+char* mir_strndup(const char *str, size_t len)
{
- if (str != NULL && len != 0) {
- char* p = (char*)mir_alloc(len + 1);
- if ( !p) {
- memcpy(p, str, len);
- p[ len ] = 0;
- }
- return p;
+ if (str == NULL || len == 0)
+ return NULL;
+
+ char *p = (char*)mir_alloc(len+1);
+ if (p) {
+ memcpy(p, str, len);
+ p[len] = 0;
}
- return NULL;
+ return p;
}
-/******************************************************************************/
-
-WCHAR* mir_wstrdup(const WCHAR* str)
+WCHAR* mir_wstrndup(const WCHAR *str, size_t len)
{
- if (str != NULL) {
- WCHAR* p = (WCHAR*)mir_alloc(sizeof(WCHAR)*(wcslen(str)+1));
- if (p)
- wcscpy(p, str);
- return p;
+ if (str == NULL || len == 0)
+ return NULL;
+
+ WCHAR *p = (WCHAR*)mir_alloc(sizeof(WCHAR)*(len+1));
+ if (p) {
+ memcpy(p, str, sizeof(WCHAR)*len);
+ p[len] = 0;
}
- return NULL;
+ return p;
}
/******************************************************************************/
@@ -187,10 +199,8 @@ WCHAR* mir_wstrdup(const WCHAR* str)
int mir_snprintf(char *buffer, size_t count, const char* fmt, ...)
{
va_list va;
- int len;
-
va_start(va, fmt);
- len = _vsnprintf(buffer, count-1, fmt, va);
+ int len = _vsnprintf(buffer, count-1, fmt, va);
va_end(va);
buffer[count-1] = 0;
return len;
@@ -198,13 +208,11 @@ int mir_snprintf(char *buffer, size_t count, const char* fmt, ...)
/******************************************************************************/
-int mir_sntprintf(TCHAR *buffer, size_t count, const TCHAR* fmt, ...)
+int mir_snwprintf(WCHAR *buffer, size_t count, const WCHAR* fmt, ...)
{
va_list va;
- int len;
-
va_start(va, fmt);
- len = _vsntprintf(buffer, count-1, fmt, va);
+ int len = _vsntprintf(buffer, count-1, fmt, va);
va_end(va);
buffer[count-1] = 0;
return len;
@@ -214,20 +222,16 @@ int mir_sntprintf(TCHAR *buffer, size_t count, const TCHAR* fmt, ...)
int mir_vsnprintf(char *buffer, size_t count, const char* fmt, va_list va)
{
- int len;
-
- len = _vsnprintf(buffer, count-1, fmt, va);
+ int len = _vsnprintf(buffer, count-1, fmt, va);
buffer[count-1] = 0;
return len;
}
/******************************************************************************/
-int mir_vsntprintf(TCHAR *buffer, size_t count, const TCHAR* fmt, va_list va)
+int mir_vsnwprintf(WCHAR *buffer, size_t count, const WCHAR* fmt, va_list va)
{
- int len;
-
- len = _vsntprintf(buffer, count-1, fmt, va);
+ int len = _vsntprintf(buffer, count-1, fmt, va);
buffer[count-1] = 0;
return len;
}
@@ -245,7 +249,7 @@ wchar_t* mir_a2u_cp(const char* src, int codepage)
return NULL;
MultiByteToWideChar(codepage, 0, src, -1, result, cbLen);
- result[ cbLen ] = 0;
+ result[cbLen] = 0;
return result;
}
@@ -253,7 +257,7 @@ wchar_t* mir_a2u_cp(const char* src, int codepage)
wchar_t* mir_a2u(const char* src)
{
- return mir_a2u_cp(src, LangPackGetDefaultCodePage());
+ return mir_a2u_cp(src, Langpack_GetDefaultCodePage());
}
/******************************************************************************/
@@ -269,7 +273,7 @@ char* mir_u2a_cp(const wchar_t* src, int codepage)
return NULL;
WideCharToMultiByte(codepage, 0, src, -1, result, cbLen, NULL, NULL);
- result[ cbLen ] = 0;
+ result[cbLen] = 0;
return result;
}
@@ -277,5 +281,5 @@ char* mir_u2a_cp(const wchar_t* src, int codepage)
char* mir_u2a(const wchar_t* src)
{
- return mir_u2a_cp(src, LangPackGetDefaultCodePage());
+ return mir_u2a_cp(src, Langpack_GetDefaultCodePage());
}