#include <stdlib.h>
#include "gterror.h"
#include "gtmem.h"

GtMem *GtMem::_instance = NULL;
pthread_mutex_t GtMem::_memMutex = PTHREAD_MUTEX_INITIALIZER;

GtMem::GtMem() {
    initMem();
}

GtMem::~GtMem() {
    if (_terminal != NULL) {
        delete _terminal;
        _terminal = NULL;
    }
    if (_runtime != NULL) {
        delete _runtime;
        _runtime = NULL;
    }
    if (_ctrlInfo != NULL) {
        delete _ctrlInfo;
        _ctrlInfo = NULL;
    }
    if (_gameRuntime != NULL) {
        delete _gameRuntime;
        _gameRuntime = NULL;
    }
}

GtMem *GtMem::instance() {
    if (_instance == NULL) {
        _instance = new GtMem();
    }
    return _instance;
}

void GtMem::initMem() {
    _terminal = new stTerminal;
    if (_terminal == NULL) {
        exit(-1);
    } else {
        memset(_terminal, 0, sizeof(stTerminal));
    }
    _runtime = new stRuntime;
    if (_runtime == NULL) {
        exit(-1);
    } else {
        memset(_runtime, 0, sizeof(stRuntime));
    }
    _ctrlInfo = new stCtrlInfo;
    if (_ctrlInfo == NULL) {
        exit(-1);
    } else {
        memset(_ctrlInfo, 0, sizeof(stCtrlInfo));
    }
    _gameRuntime = new stGameRuntime;
    if (_gameRuntime == NULL) {
        exit(-1);
    } else {
        memset(_gameRuntime, 0, sizeof(stGameRuntime));
    }
}

/************************************************************
*功能：保存终端配置的内存数据
*输入：data=终端配置的内存数据
*输出：无
*返回：!=0错误
*/
int GtMem::setMemTerminal(stTerminal *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(_terminal, data, sizeof(stTerminal));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：获取终端配置的内存数据
*输入：无
*输出：data=终端配置的内存数据
*返回：!=0错误
*/
int GtMem::getMemTerminal(stTerminal *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(data, _terminal, sizeof(stTerminal));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：保存终端运行的内存数据
*输入：data=终端运行的内存数据
*输出：无
*返回：!=0错误
*/
int GtMem::setMemRuntime(stRuntime *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(_runtime, data, sizeof(stRuntime));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：获取终端运行的内存数据
*输入：无
*输出：data=终端运行的内存数据
*返回：!=0错误
*/
int GtMem::getMemRuntime(stRuntime *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(data, _runtime, sizeof(stRuntime));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：保存控制参数内存数据
*输入：data=控制参数内存数据
*输出：无
*返回：!=0错误
*/
int GtMem::setMemCtrlInfo(stCtrlInfo *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(_ctrlInfo, data, sizeof(stCtrlInfo));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：获取控制参数内存数据
*输入：无
*输出：data=控制参数内存数据
*返回：!=0错误
*/
int GtMem::getMemCtrlInfo(stCtrlInfo *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(data, _ctrlInfo, sizeof(stCtrlInfo));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：保存游戏内存数据
*输入：data=游戏内存数据
*输出：无
*返回：!=0错误
*/
int GtMem::setMemGameRuntime(stGameRuntime *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(_gameRuntime, data, sizeof(stGameRuntime));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}

/************************************************************
*功能：获取游戏内存数据
*输入：无
*输出：data=游戏内存数据
*返回：!=0错误
*/
int GtMem::getMemGameRuntime(stGameRuntime *data) {
    int result = 0;

    if (data == NULL) {
        return GtError::ERROR_INVALID_FUNC_PARAM;
    }
    result = pthread_mutex_lock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_LOCK_FAILED;
    }
    memcpy(data, _gameRuntime, sizeof(stGameRuntime));
    result = pthread_mutex_unlock(&_memMutex);
    if (result != 0) {
        return GtError::ERROR_MEM_UNLOCK_FAILED;
    }
    return 0;
}
