2024-11-06 stan

init_balance,init_update, init_trade初始化分析

1. init_balance() 函数分析

1.1 基础设施定义

dict_t *dict_balance;
static dict_t *dict_asset;
struct asset_type {
    int prec_save;
    int prec_show;
};

1.2 字典操作函数实现

static uint32_t asset_dict_hash_function(const void *key)
{
    return dict_generic_hash_function(key, strlen(key));
}
}
static void *asset_dict_key_dup(const void *key)
{
    return strdup(key);
}
}
static void *asset_dict_val_dup(const void *val)
{
    struct asset_type *obj = malloc(sizeof(struct asset_type));
    if (obj == NULL)
        return NULL;
    memcpy(obj, val, sizeof(struct asset_type));
    return obj;
}
}
static int asset_dict_key_compare(const void *key1, const void *key2)
{
    return strcmp(key1, key2);
}
}
static void asset_dict_key_free(void *key)
{
    free(key);
}
}
static void asset_dict_val_free(void *val)
{
    free(val);
}

主要功能:

  1. balance_dict 相关函数:
static uint32_t balance_dict_hash_function(const void *key)
{
    return dict_generic_hash_function(key, sizeof(struct balance_key));
}

1.3 init_balance() 执行流程