Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Refcount220323
//gcc 7.4.0 #include <assert.h> #include <stdlib.h> #include <stdbool.h> #include <stdio.h> typedef struct _tagAPPLICATION APPLICATION, *PAPPLICATION; typedef struct _tagREFCOUNT REFCOUNT, *PREFCOUNT; struct _tagREFCOUNT { void (*deleter)(void *); void *ptr; int count; }; struct _tagAPPLICATION { REFCOUNT refCount; char *str; int nInfoGatherCnt; int nTigerHarmCnt; }; void Application_Deleter(PAPPLICATION); static inline void Application_Pull(PAPPLICATION); static inline void RefCount_SetDeleter(PREFCOUNT refCount, void (*deleter)(void *)) { refCount->deleter = deleter; } static inline void RefCount_SetPtr(PREFCOUNT refCount, void* ptr) { refCount->ptr = ptr; } static inline void RefCount_Init(PREFCOUNT refCount, void* ptr, void (*deleter)(void *)) { refCount->ptr = ptr; refCount->deleter = deleter; refCount->count = 1; } static inline void RefCount_Increase(PREFCOUNT refCount) { ++refCount->count; } static inline void RefCount_Decrease(PREFCOUNT refCount) { if ((--refCount->count) == 0) { refCount->deleter(refCount->ptr); } } PAPPLICATION *Application__get() { static PAPPLICATION g_pApp; return &g_pApp; } void Application_Initialize(PAPPLICATION pApp) { pApp->str = "Initialized"; } PAPPLICATION Application_Create() { PAPPLICATION *ppApp = NULL; ppApp = Application__get(); if (!ppApp || *ppApp) { assert(false); return NULL; } *ppApp = calloc(1, sizeof(APPLICATION)); RefCount_Init(&(*ppApp)->refCount, *ppApp, (void (*)(void *)) Application_Deleter); Application_Initialize(*ppApp); return *ppApp; } PAPPLICATION Application_GetInstance() { PAPPLICATION *ppApp; ppApp = Application__get(); if (!*ppApp) { assert(false); return NULL; } Application_Pull(*ppApp); return *ppApp; } const char *Application_GetString(PAPPLICATION pApp) { return pApp->str; } void Application_ChangeState(PAPPLICATION pApp) { pApp->str = "Blue Archive"; } void Application_Deleter(PAPPLICATION pApp) { PAPPLICATION *ppApp; ppApp = Application__get(); if (ppApp) { *ppApp = NULL; } printf("Destroying application...\n"); free(pApp); } static inline void Application_Put(PAPPLICATION pApp) { RefCount_Decrease(&(pApp->refCount)); } static inline void Application_Pull(PAPPLICATION pApp) { RefCount_Increase(&(pApp->refCount)); } void GetInfo() { PAPPLICATION pApp; pApp = Application_GetInstance(); printf("Information: Gathered %d times.\n", ++pApp->nInfoGatherCnt); Application_Put(pApp); } static inline int Application_GetTigerHarmCount(PAPPLICATION pApp) { return pApp->nTigerHarmCnt; } static inline void Application_IncreaseTigerHarmCount(PAPPLICATION pApp) { ++pApp->nTigerHarmCnt; } void HarmTiger() { PAPPLICATION pApp; pApp = Application_GetInstance(); printf("Tiger harmed: %d times.\n", Application_GetTigerHarmCount(pApp)); Application_IncreaseTigerHarmCount(pApp); Application_Put(pApp); } int main(void) { PAPPLICATION pApp = Application_Create(); for (size_t i = 0; i < 10; ++i) { GetInfo(); } for (size_t i = 0; i < 10; ++i) { HarmTiger(); } Application_Put(pApp); return 0; }
run
|
edit
|
history
|
help
0
Exploiting uninitialized variable
Str_ptr_arg
Lab 8 part 2 v.05
MinMaxArray
Memoria dinamica no funciona ;C
FILE_Access
Assignment 5 part 2 v2
Simple interest
18BCE2182 ASSESS_2 Q-1
Class Friday 1/20 Inauguration Day