a4_0002
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
* acl4のページ0002
-(by [[K]], 2026.01.24)
** (1)
#if (a_Version >= 1)
#define List a_List
#define List_ini a_List_ini
#define List_addTl a_List_addTl
#define List_addHd a_List_addHd
#define List_rmv a_List_rmv
#endif
a_class(a_List) { a_List *prv, *nxt; };
a_static void a_List_ini(a_List *w) { w->prv = w->nxt = ...
a_static void a_List_add(a_List *prv, a_List *tgt, a_Lis...
{
prv->nxt = tgt;
tgt->prv = prv;
tgt->nxt = nxt;
nxt->prv = tgt;
}
a_static void a_List_rmv(a_List *prv, a_List *nxt) { prv...
a_static void a_List_addTl(a_List *w, a_List *tgt) { a_L...
a_static void a_List_addHd(a_List *w, a_List *tgt) { a_L...
#if (a_Version >= 1)
#define malloc_ a_malloc
#define free_ a_free
#define realloc_ a_realloc
#endif
a_class(a_malloc_Header) {
a_List lst[1];
intptr_t dmy[1], sz, lin;
const char *fil;
intptr_t serialNum, sig;
};
a_malloc_Header a_malloc_hed0[1];
#define a_malloc_Sig 0xff0055aa
a_static void *a_malloc(_argDef_ intptr_t sz)
{
#if (a_DbgLv < 2)
void *p = malloc(sz);
if (p == NULL)
a_errExit("malloc_: out of memory: sz=%d");
return p;
#else
if (sz < 0)
a_errExit("%s(%d): malloc_ size error: sz=%d...
a_malloc_Header *h = (a_malloc_Header *) malloc(...
if (h == NULL)
a_errExit("%s(%d): malloc_: out of memory: s...
if (a_malloc_hed0->lst->prv == NULL) {
a_List_ini(a_malloc_hed0->lst);
a_malloc_hed0->sz = -1;
a_malloc_hed0->sig = a_malloc_Sig;
a_malloc_hed0->lin = 0;
}
a_List_addTl(a_malloc_hed0->lst, h->lst);
h->sz = sz;
h->sig = a_malloc_Sig;
h->lin = a_lin;
h->fil = a_fil;
h->serialNum = ++(a_malloc_hed0->serialNum);
a_malloc_hed0->lin++;
return h + 1;
#endif
}
a_static void a_free(_argDef_ void *p, intptr_t sz)
{
#if (a_DbgLv < 2)
(void) sz;
free(p);
#else
if (p == NULL) return;
a_malloc_Header *h = (a_malloc_Header *) p - 1;
if (h->sig != a_malloc_Sig)
a_errExit("%s(%d): free_: bad signature", a_...
if (h->sz != sz)
a_errExit("%s(%d): free_: bad size: sz=%d, a...
h->sig = 0;
h->serialNum = 0;
a_List_rmv(h->lst->prv, h->lst->nxt);
a_malloc_hed0->lin--;
free(h);
#endif
}
a_static void *a_realloc(_argDef_ void *p, intptr_t sz0...
{
#if (a_DbgLv < 2)
(void) sz0;
p = realloc(p, sz1);
if (p == NULL)
a_errExit("realloc_: out of memory: sz1=%d");
return p;
#else
if (sz1 < 0)
a_errExit("%s(%d): realloc_ size error: sz1=...
if (p == NULL)
return a_malloc(a_fil, a_lin, sz1);
a_malloc_Header *h = (a_malloc_Header *) p - 1;
if (h->sig != a_malloc_Sig)
a_errExit("%s(%d): realloc_: bad signature",...
if (h->sz != sz0)
a_errExit("%s(%d): realloc_: bad size: sz=%d...
h->sig = 0;
intptr_t sn = h->serialNum;
h->serialNum = 0;
a_List *prv = h->lst->prv;
a_List *nxt = h->lst->nxt;
a_List_rmv(prv, nxt);
a_malloc_Header *h1 = (a_malloc_Header *) reallo...
if (h1 == NULL)
a_errExit("%s(%d): realloc_: out of memory: ...
a_List_add(prv, h1->lst, nxt);
h1->sig = a_malloc_Sig;
h1->serialNum = sn;
h1->sz = sz1;
h1->lin = a_lin;
h1->fil = a_fil;
return h1 + 1;
#endif
}
a_static intptr_t a_malloc_totalSiz(_argDef_ intptr_t *...
{
intptr_t s = 0;
#if (a_DbgLv >= 2)
(void) a_fil; (void) a_lin;
if (a_malloc_hed0->lst->prv != NULL) {
a_List *t = a_malloc_hed0->lst->nxt;
for (;;) {
a_malloc_Header *h = (a_malloc_Header *)...
if (h->sz < 0) break;
s += h->sz;
t = t->nxt;
}
}
#endif
if (pn != NULL) *pn = a_malloc_hed0->lin;
return s;
}
a_static void a_malloc_debugList(_argDef)
{
#if (a_DbgLv >= 2)
fprintf(stderr, "%s(%d): malloc_debugList()\n", ...
if (a_malloc_hed0->lst->prv != NULL) {
a_List *t = a_malloc_hed0->lst->nxt;
for (;;) {
a_malloc_Header *h = (a_malloc_Header *)...
if (h->sz < 0) break;
fprintf(stderr, " [p:%08x sz:%d / %s(%d...
t = t->nxt;
}
}
#endif
}
** (2) List
-私の大好きな双方向リストを簡単に扱うためのライブラリ関数...
-まず a_class(a_List) { a_List *prv, *nxt; }; で構造体を...
-最初なので、これがどう展開されるかを説明します。
typedef struct a_List ## _ a_List;
struct a_List ## _ { a_List *prv, *nxt; };
-となったあと、
typedef struct a_List_ a_List;
struct a_List_ { a_List *prv, *nxt; };
-となるのです。これで自前でtypedefしなくてもa_Listをstruc...
-私はよく使う語に関しては短く書くのが好きです。
--prv → previous
--nxt → next
--ini → init
--ins → insert
--rmv → remove
--tgt → target
--Tl → Tail
--Hd → Head
-またクラスのthisポインタのことをwと略します。これは私が...
-こんな簡単な処理くらい、関数化しないで手書きしてもいいで...
-addTl, addHdの第一引数wは番兵用のノードで、絶対に削除さ...
-この番兵ノードのnxtが双方向リストの先頭で、番兵ノードのp...
** (3) malloc_, free_, realloc_
-まず a_DbgLv が 0 だったらどうなるかを確認してください。
-これらは malloc, free, realloc に out of memory 用のチェ...
-さて、では a_DbgLv が 2 だった場合を見てみましょう。
-まず malloc ではサイズ指定が符号付きの整数になっていて、...
-そもそも私は、size_t型がunsignedであるのは下手な設計だと...
-size_tがunsignedなのは、メモリ領域のサイズとして2GB以上...
-mallocではシグネチャやシリアルナンバーをヘッダに記入する...
-freeではヘッダの健全性をチェックする処理が追加されます。...
-シリアルナンバーは、この領域をfreeしてmallocで偶然に再利...
-このプログラムではまだシリアルナンバーを全く利用できてな...
-mallocでは引数の最初に、 _argDef_ という見慣れないもの...
-これは何かというと、 malloc_ を呼び出すときに _arg_ をつ...
-acl4の free_ ではサイズ指定が必要です。これで「領域Aを開...
-a_malloc_totalSiz と a_malloc_debugList は、これをこのま...
** (4) 使い方
-acl4ライブラリは以下のようにして利用します。
-[1]まずa4_0001~a4_0002のプログラムをつなげて"acl4.c"と...
-[2]次にacl4を使ったプログラムを書きます。
-[3]コンパイル時に、"acl4.c"のあるパスをインクルードパス...
-以下のコードでやってみます。"t0002a.c"とします。
#define a_Version 1
#include <acl4.c>
int main()
{
char *p = malloc_(_arg_ 12);
char *q = malloc_(_arg_ 34);
char *r = malloc_(_arg_ 56);
char *s = malloc_(_arg_ 78);
free_(_arg_ q, 34);
r = realloc_(_arg_ r, 56, 90);
a_malloc_debugList(_arg);
return 0;
}
>t0002a
t0002a.c(12): malloc_debugList()
[p:00d2cb38 sz:12 / t0002a.c(6)]
[p:00d330e8 sz:90 / t0002a.c(11)]
[p:00d33070 sz:78 / t0002a.c(9)]
** (5)
-私の信条としては、C言語の標準関数が最初からこのくらいの...
--いや、生まれたかもしれないけど、ここまで活用されなかっ...
-こんな簡単な仕組みがあればそれで十分だったのです。
-バグに早く気付くことができれば、複雑な仕組みがなくても何...
** (99) 更新履歴
-2026.01.24(土) 初版。
-2026.01.30(金) クラス名変更: DoublyLinkList → List
終了行:
* acl4のページ0002
-(by [[K]], 2026.01.24)
** (1)
#if (a_Version >= 1)
#define List a_List
#define List_ini a_List_ini
#define List_addTl a_List_addTl
#define List_addHd a_List_addHd
#define List_rmv a_List_rmv
#endif
a_class(a_List) { a_List *prv, *nxt; };
a_static void a_List_ini(a_List *w) { w->prv = w->nxt = ...
a_static void a_List_add(a_List *prv, a_List *tgt, a_Lis...
{
prv->nxt = tgt;
tgt->prv = prv;
tgt->nxt = nxt;
nxt->prv = tgt;
}
a_static void a_List_rmv(a_List *prv, a_List *nxt) { prv...
a_static void a_List_addTl(a_List *w, a_List *tgt) { a_L...
a_static void a_List_addHd(a_List *w, a_List *tgt) { a_L...
#if (a_Version >= 1)
#define malloc_ a_malloc
#define free_ a_free
#define realloc_ a_realloc
#endif
a_class(a_malloc_Header) {
a_List lst[1];
intptr_t dmy[1], sz, lin;
const char *fil;
intptr_t serialNum, sig;
};
a_malloc_Header a_malloc_hed0[1];
#define a_malloc_Sig 0xff0055aa
a_static void *a_malloc(_argDef_ intptr_t sz)
{
#if (a_DbgLv < 2)
void *p = malloc(sz);
if (p == NULL)
a_errExit("malloc_: out of memory: sz=%d");
return p;
#else
if (sz < 0)
a_errExit("%s(%d): malloc_ size error: sz=%d...
a_malloc_Header *h = (a_malloc_Header *) malloc(...
if (h == NULL)
a_errExit("%s(%d): malloc_: out of memory: s...
if (a_malloc_hed0->lst->prv == NULL) {
a_List_ini(a_malloc_hed0->lst);
a_malloc_hed0->sz = -1;
a_malloc_hed0->sig = a_malloc_Sig;
a_malloc_hed0->lin = 0;
}
a_List_addTl(a_malloc_hed0->lst, h->lst);
h->sz = sz;
h->sig = a_malloc_Sig;
h->lin = a_lin;
h->fil = a_fil;
h->serialNum = ++(a_malloc_hed0->serialNum);
a_malloc_hed0->lin++;
return h + 1;
#endif
}
a_static void a_free(_argDef_ void *p, intptr_t sz)
{
#if (a_DbgLv < 2)
(void) sz;
free(p);
#else
if (p == NULL) return;
a_malloc_Header *h = (a_malloc_Header *) p - 1;
if (h->sig != a_malloc_Sig)
a_errExit("%s(%d): free_: bad signature", a_...
if (h->sz != sz)
a_errExit("%s(%d): free_: bad size: sz=%d, a...
h->sig = 0;
h->serialNum = 0;
a_List_rmv(h->lst->prv, h->lst->nxt);
a_malloc_hed0->lin--;
free(h);
#endif
}
a_static void *a_realloc(_argDef_ void *p, intptr_t sz0...
{
#if (a_DbgLv < 2)
(void) sz0;
p = realloc(p, sz1);
if (p == NULL)
a_errExit("realloc_: out of memory: sz1=%d");
return p;
#else
if (sz1 < 0)
a_errExit("%s(%d): realloc_ size error: sz1=...
if (p == NULL)
return a_malloc(a_fil, a_lin, sz1);
a_malloc_Header *h = (a_malloc_Header *) p - 1;
if (h->sig != a_malloc_Sig)
a_errExit("%s(%d): realloc_: bad signature",...
if (h->sz != sz0)
a_errExit("%s(%d): realloc_: bad size: sz=%d...
h->sig = 0;
intptr_t sn = h->serialNum;
h->serialNum = 0;
a_List *prv = h->lst->prv;
a_List *nxt = h->lst->nxt;
a_List_rmv(prv, nxt);
a_malloc_Header *h1 = (a_malloc_Header *) reallo...
if (h1 == NULL)
a_errExit("%s(%d): realloc_: out of memory: ...
a_List_add(prv, h1->lst, nxt);
h1->sig = a_malloc_Sig;
h1->serialNum = sn;
h1->sz = sz1;
h1->lin = a_lin;
h1->fil = a_fil;
return h1 + 1;
#endif
}
a_static intptr_t a_malloc_totalSiz(_argDef_ intptr_t *...
{
intptr_t s = 0;
#if (a_DbgLv >= 2)
(void) a_fil; (void) a_lin;
if (a_malloc_hed0->lst->prv != NULL) {
a_List *t = a_malloc_hed0->lst->nxt;
for (;;) {
a_malloc_Header *h = (a_malloc_Header *)...
if (h->sz < 0) break;
s += h->sz;
t = t->nxt;
}
}
#endif
if (pn != NULL) *pn = a_malloc_hed0->lin;
return s;
}
a_static void a_malloc_debugList(_argDef)
{
#if (a_DbgLv >= 2)
fprintf(stderr, "%s(%d): malloc_debugList()\n", ...
if (a_malloc_hed0->lst->prv != NULL) {
a_List *t = a_malloc_hed0->lst->nxt;
for (;;) {
a_malloc_Header *h = (a_malloc_Header *)...
if (h->sz < 0) break;
fprintf(stderr, " [p:%08x sz:%d / %s(%d...
t = t->nxt;
}
}
#endif
}
** (2) List
-私の大好きな双方向リストを簡単に扱うためのライブラリ関数...
-まず a_class(a_List) { a_List *prv, *nxt; }; で構造体を...
-最初なので、これがどう展開されるかを説明します。
typedef struct a_List ## _ a_List;
struct a_List ## _ { a_List *prv, *nxt; };
-となったあと、
typedef struct a_List_ a_List;
struct a_List_ { a_List *prv, *nxt; };
-となるのです。これで自前でtypedefしなくてもa_Listをstruc...
-私はよく使う語に関しては短く書くのが好きです。
--prv → previous
--nxt → next
--ini → init
--ins → insert
--rmv → remove
--tgt → target
--Tl → Tail
--Hd → Head
-またクラスのthisポインタのことをwと略します。これは私が...
-こんな簡単な処理くらい、関数化しないで手書きしてもいいで...
-addTl, addHdの第一引数wは番兵用のノードで、絶対に削除さ...
-この番兵ノードのnxtが双方向リストの先頭で、番兵ノードのp...
** (3) malloc_, free_, realloc_
-まず a_DbgLv が 0 だったらどうなるかを確認してください。
-これらは malloc, free, realloc に out of memory 用のチェ...
-さて、では a_DbgLv が 2 だった場合を見てみましょう。
-まず malloc ではサイズ指定が符号付きの整数になっていて、...
-そもそも私は、size_t型がunsignedであるのは下手な設計だと...
-size_tがunsignedなのは、メモリ領域のサイズとして2GB以上...
-mallocではシグネチャやシリアルナンバーをヘッダに記入する...
-freeではヘッダの健全性をチェックする処理が追加されます。...
-シリアルナンバーは、この領域をfreeしてmallocで偶然に再利...
-このプログラムではまだシリアルナンバーを全く利用できてな...
-mallocでは引数の最初に、 _argDef_ という見慣れないもの...
-これは何かというと、 malloc_ を呼び出すときに _arg_ をつ...
-acl4の free_ ではサイズ指定が必要です。これで「領域Aを開...
-a_malloc_totalSiz と a_malloc_debugList は、これをこのま...
** (4) 使い方
-acl4ライブラリは以下のようにして利用します。
-[1]まずa4_0001~a4_0002のプログラムをつなげて"acl4.c"と...
-[2]次にacl4を使ったプログラムを書きます。
-[3]コンパイル時に、"acl4.c"のあるパスをインクルードパス...
-以下のコードでやってみます。"t0002a.c"とします。
#define a_Version 1
#include <acl4.c>
int main()
{
char *p = malloc_(_arg_ 12);
char *q = malloc_(_arg_ 34);
char *r = malloc_(_arg_ 56);
char *s = malloc_(_arg_ 78);
free_(_arg_ q, 34);
r = realloc_(_arg_ r, 56, 90);
a_malloc_debugList(_arg);
return 0;
}
>t0002a
t0002a.c(12): malloc_debugList()
[p:00d2cb38 sz:12 / t0002a.c(6)]
[p:00d330e8 sz:90 / t0002a.c(11)]
[p:00d33070 sz:78 / t0002a.c(9)]
** (5)
-私の信条としては、C言語の標準関数が最初からこのくらいの...
--いや、生まれたかもしれないけど、ここまで活用されなかっ...
-こんな簡単な仕組みがあればそれで十分だったのです。
-バグに早く気付くことができれば、複雑な仕組みがなくても何...
** (99) 更新履歴
-2026.01.24(土) 初版。
-2026.01.30(金) クラス名変更: DoublyLinkList → List
ページ名: