From e0d2405a3093e603867d2cc32c7557f62c3ede71 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Mon, 22 Feb 2010 12:25:23 +0000 Subject: [PATCH] Rework data structures allocations / frees. - New mifare_*_tag_new() functions for allocating and initialising memory for a given MIFARE tag; - Rename mifare_*_free_tag() to mifare_*_tag_free() for consistent names with mifare_*_tag_new() functions. --- libfreefare/freefare.c | 10 +++++----- libfreefare/freefare_internal.h | 6 ++++-- libfreefare/mifare_classic.c | 29 ++++++++++++++++++++++------- libfreefare/mifare_ultralight.c | 29 ++++++++++++++++++++++------- 4 files changed, 53 insertions(+), 21 deletions(-) diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index cf5fe13..e436b44 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -109,10 +109,10 @@ freefare_get_tags (nfc_device_t *device) switch (type) { case CLASSIC_1K: case CLASSIC_4K: - tags[tag_count-1] = malloc (sizeof (struct mifare_classic_tag)); + tags[tag_count-1] = mifare_classic_tag_new (); break; case ULTRALIGHT: - tags[tag_count-1] = malloc (sizeof (struct mifare_ultralight_tag)); + tags[tag_count-1] = mifare_ultralight_tag_new (); break; } @@ -121,7 +121,7 @@ freefare_get_tags (nfc_device_t *device) /* * Initialize common fields - * (Target specific fields are initialized in mifare_*_connect()) + * (Target specific fields are initialized in mifare_*_tag_new()) */ (tags[tag_count-1])->device = device; (tags[tag_count-1])->info = target_info.nai; @@ -155,10 +155,10 @@ freefare_free_tags (MifareTag *tags) switch (tags[i]->type) { case CLASSIC_1K: case CLASSIC_4K: - mifare_classic_free_tag (tags[i]); + mifare_classic_tag_free (tags[i]); break; case ULTRALIGHT: - mifare_ultralight_free_tag (tags[i]); + mifare_ultralight_tag_free (tags[i]); break; } } diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index c195a27..8e99567 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -24,8 +24,10 @@ struct mad_sector_0x00; struct mad_sector_0x10; void crc8 (uint8_t *crc, const uint8_t value); -void mifare_classic_free_tag (MifareTag tag); -void mifare_ultralight_free_tag (MifareTag tag); +MifareTag mifare_classic_tag_new (void); +void mifare_classic_tag_free (MifareTag tag); +MifareTag mifare_ultralight_tag_new (void); +void mifare_ultralight_tag_free (MifareTag tag); uint8_t sector_0x00_crc8 (Mad mad); uint8_t sector_0x10_crc8 (Mad mad); diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c index 56fa0f8..46045d0 100644 --- a/libfreefare/mifare_classic.c +++ b/libfreefare/mifare_classic.c @@ -131,22 +131,37 @@ int get_block_access_bits (MifareTag tag, const MifareClassicBlockNumber block, /* - * MIFARE card communication preparation functions - * - * The following functions send NFC commands to the initiator to prepare - * communication with a MIFARE card, and perform required cleannups after using - * the target. + * Memory management functions. */ /* - * Free the provided tag list. + * Allocates and initialize a MIFARE Classic tag. + */ + +MifareTag +mifare_classic_tag_new (void) +{ + return malloc (sizeof (struct mifare_classic_tag)); +} + +/* + * Free the provided tag. */ void -mifare_classic_free_tag (MifareTag tag) +mifare_classic_tag_free (MifareTag tag) { free (tag); } + +/* + * MIFARE card communication preparation functions + * + * The following functions send NFC commands to the initiator to prepare + * communication with a MIFARE card, and perform required cleanups after using + * the target. + */ + /* * Establish connection to the provided tag. */ diff --git a/libfreefare/mifare_ultralight.c b/libfreefare/mifare_ultralight.c index 7b73686..13f2a08 100644 --- a/libfreefare/mifare_ultralight.c +++ b/libfreefare/mifare_ultralight.c @@ -43,22 +43,37 @@ /* - * MIFARE card communication preparation functions - * - * The following functions send NFC commands to the initiator to prepare - * communication with a MIFARE card, and perform required cleannups after using - * the target. + * Memory management functions. */ /* - * Free the provided tag list. + * Allocates and initialize a MIFARE UltraLight tag. + */ +MifareTag +mifare_ultralight_tag_new (void) +{ + return malloc (sizeof (struct mifare_ultralight_tag)); +} + +/* + * Free the provided tag. */ void -mifare_ultralight_free_tag (MifareTag tag) +mifare_ultralight_tag_free (MifareTag tag) { free (tag); } + +/* + * MIFARE card communication preparation functions + * + * The following functions send NFC commands to the initiator to prepare + * communication with a MIFARE card, and perform required cleanups after using + * the target. + */ + + /* * Establish connection to the provided tag. */