blob: a2e596a7e10c51f3512746cc153c23e71d7a160d [file] [log] [blame]
Jared Duke13689fe2019-04-16 16:22:07 -04001/* Copyright 2019 Google LLC. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040016#include "allocator.h"
17
Benoit Jacob607e4452019-08-21 15:37:39 -040018#include <cstdint>
Benoit Jacobb21b3102019-04-15 11:52:05 -040019#include <cstdlib>
20
21#ifdef _WIN32
22#include <malloc.h>
23#endif
24
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040025namespace ruy {
26
Sean Silvad8bbade2019-04-12 12:32:11 -040027namespace detail {
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040028
Sean Silvac85f6d72019-12-03 13:42:15 -050029void *SystemAlignedAlloc(std::ptrdiff_t num_bytes) {
Benoit Jacobb21b3102019-04-15 11:52:05 -040030#ifdef _WIN32
Sean Silvac85f6d72019-12-03 13:42:15 -050031 return _aligned_malloc(num_bytes, kMinimumBlockAlignment);
Benoit Jacobb21b3102019-04-15 11:52:05 -040032#else
Sean Silvad8bbade2019-04-12 12:32:11 -040033 void *ptr;
Sean Silvac85f6d72019-12-03 13:42:15 -050034 if (posix_memalign(&ptr, kMinimumBlockAlignment, num_bytes)) {
Sean Silvad8bbade2019-04-12 12:32:11 -040035 return nullptr;
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040036 }
Sean Silvad8bbade2019-04-12 12:32:11 -040037 return ptr;
Benoit Jacobb21b3102019-04-15 11:52:05 -040038#endif
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040039}
40
Sean Silvac85f6d72019-12-03 13:42:15 -050041void SystemAlignedFree(void *ptr) {
Benoit Jacobb21b3102019-04-15 11:52:05 -040042#ifdef _WIN32
43 _aligned_free(ptr);
44#else
45 free(ptr);
46#endif
47}
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040048
Sean Silvad8bbade2019-04-12 12:32:11 -040049} // namespace detail
Benoit Jacoba0ba3ac2019-04-08 12:00:37 -040050
51} // namespace ruy