kokoro.best / cpp /src /utils /memory_utils.hpp
inoryQwQ's picture
Upload folder using huggingface_hub (part 3)
71e354e verified
Raw
History Blame Contribute Delete
2.35 kB
/**************************************************************************************************
*
* Copyright (c) 2019-2026 Axera Semiconductor (Ningbo) Co., Ltd. All Rights Reserved.
*
* This source file is the property of Axera Semiconductor (Ningbo) Co., Ltd. and
* may not be copied or distributed in any isomorphic form without the prior
* written consent of Axera Semiconductor (Ningbo) Co., Ltd.
*
**************************************************************************************************/
#pragma once
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <string.h>
#include <unistd.h>
namespace utils {
bool file_exist(const std::string &path);
bool read_file(const std::string &path, std::vector<char> &data);
bool read_file(const std::string &path, char **data, size_t *len);
}
class MMap
{
private:
void *_add = nullptr;
int _size;
public:
MMap() {}
MMap(const char *file)
{
open_file(file);
}
~MMap()
{
close_file();
}
bool open_file(const char *file)
{
_add = _mmap(file, &_size);
if (!_add)
{
return false;
}
return true;
}
void close_file()
{
if (_add)
{
munmap(_add, _size);
_add = nullptr;
_size = 0;
}
}
size_t size()
{
return _size;
}
void *data()
{
return _add;
}
static void *_mmap(const char *model_file, int *model_size)
{
auto *file_fp = fopen(model_file, "r");
if (!file_fp)
{
return nullptr;
}
fseek(file_fp, 0, SEEK_END);
*model_size = ftell(file_fp);
fclose(file_fp);
int fd = open(model_file, O_RDONLY);
if (fd < 0)
{
fprintf(stderr, "[MMap] open failed for file %s: %s\n", model_file, strerror(errno));
return nullptr;
}
void *mmap_addr = mmap(NULL, *model_size, PROT_READ, MAP_SHARED, fd, 0);
if (mmap_addr == MAP_FAILED)
{
fprintf(stderr, "[MMap] mmap failed for file %s: %s\n", model_file, strerror(errno));
close(fd);
return nullptr;
}
close(fd);
return mmap_addr;
}
};