Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / src / gna_plugin / util.cpp
1 // Copyright (C) 2018-2019 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // util.cpp : various utility functions for debugging, file i/o, etc.
5 //
6
7 #include <cinttypes>
8 #ifndef _WIN32
9 #include <mm_malloc.h>
10 #endif
11 #include <cstring>
12 #include <details/ie_exception.hpp>
13 #include "util.h"
14 #include "gna_plugin_log.hpp"
15
16 void *AllocateMemory(uint32_t num_memory_bytes, const char *ptr_name) {
17     void *ptr_memory = _mm_malloc(num_memory_bytes, 64);
18     if (ptr_memory == NULL) {
19         THROW_GNA_EXCEPTION << "Memory allocation failed for " << ptr_name;
20     }
21     memset(ptr_memory, 0, num_memory_bytes);
22
23     return (ptr_memory);
24 }
25
26 void FreeMemory(void *ptr_memory) {
27     if (ptr_memory != NULL) {
28         _mm_free(ptr_memory);
29     }
30     ptr_memory = NULL;
31 }
32
33 int32_t MemoryOffset(void *ptr_target, void *ptr_base) {
34     uint64_t target = (uint64_t) ptr_target;
35     uint64_t base = (uint64_t) ptr_base;
36     if (target == 0) {  // handle NULL pointers separately
37         return (-1);
38     } else if (target < base) {
39         THROW_GNA_EXCEPTION << "Error:  target address value " <<  target<< " is less than base address " << base << " in MemoryOffset()";
40     } else {
41         uint64_t diff = target - base;
42         if (diff > 0x7fffffff) {
43             THROW_GNA_EXCEPTION << "Error:  target address value " << target << " too far from base address " << base << " in MemoryOffset()!";
44         }
45         return ((int32_t) diff);
46     }
47 }
48