Publishing 2019 R1.1 content and Myriad plugin sources (#162)
[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
5 #include <cinttypes>
6 #ifndef _WIN32
7 #include <mm_malloc.h>
8 #endif
9 #include <cstring>
10 #include <details/ie_exception.hpp>
11 #include "util.h"
12 #include "gna_plugin_log.hpp"
13
14 void *AllocateMemory(uint32_t num_memory_bytes, const char *ptr_name) {
15     void *ptr_memory = _mm_malloc(num_memory_bytes, 64);
16     if (ptr_memory == NULL) {
17         THROW_GNA_EXCEPTION << "Memory allocation failed for " << ptr_name;
18     }
19     memset(ptr_memory, 0, num_memory_bytes);
20
21     return (ptr_memory);
22 }
23
24 void FreeMemory(void *ptr_memory) {
25     if (ptr_memory != NULL) {
26         _mm_free(ptr_memory);
27     }
28     ptr_memory = NULL;
29 }
30
31 int32_t MemoryOffset(void *ptr_target, void *ptr_base) {
32     uint64_t target = (uint64_t) ptr_target;
33     uint64_t base = (uint64_t) ptr_base;
34     if (target == 0) {  // handle NULL pointers separately
35         return (-1);
36     } else if (target < base) {
37         THROW_GNA_EXCEPTION << "Error:  target address value " <<  target<< " is less than base address " << base << " in MemoryOffset()";
38     } else {
39         uint64_t diff = target - base;
40         if (diff > 0x7fffffff) {
41             THROW_GNA_EXCEPTION << "Error:  target address value " << target << " too far from base address " << base << " in MemoryOffset()!";
42         }
43         return ((int32_t) diff);
44     }
45 }
46