mohandev2 / openhpi_old

Other
0 stars 0 forks source link

some comments about rpt_util #2100

Closed mohandev2 closed 6 years ago

mohandev2 commented 20 years ago

I'd like to speak something about rpt_util 1. remove ResourceState struct because we don't use it again.

2. We needn't define RPT_ENTRY_BEGIN and RDR_BEGIN. We can use HPI define a. SAHPI_FIRST_ENTRY b. SAHPI_LAST_ENTRY we needn't FREE_RPT_DATA and KEEP_RPT_DATA define

3. owndata field is redundancy, we can remove it typedef struct { SaHpiRptEntryT rpt_entry; int owndata;/*remove it*/ void *data; GSList *rdrtable;
} RPTEntry; if you don't use private data, you can set data as NULL. so we can remove owndata field. other structs have same situations. all the owndata fields can be removed. typedef struct { SaHpiRptEntryT rpt_entry; int owndata;/*remove it*/ void *data; GSList *rdrtable; } RPTEntry;

typedef struct { SaHpiRdrT rdr; int owndata;/*remove it*/ void *data; } RDRecord;

4. change oh_flush_rpt to oh_free_all_rpt(or other name) because this function name doesn't make sense.

5. add some features

5.1 new structure define typedef struct { SaHpiRptInfoT rpt_info; GSList *rptable;

/* free private data you can set it as NULL if you needn't release the private data */ void (*res_private_free)(void *);

/* free private data you can set it as NULL if you needn't release the private data */ void (*rdr_private_free)(void *);

/*if data in struct RPTEntry is a key as ResourceID if you needn't compare the resource , you can set the function as NULL. or if you think that only both pointer is equal, the rdr is equal.

*/ int (*res_cmp)(void* res1, void* res2);

/*

if data in struct RDRecord is a key as ResourceID if you needn't compare the rdr , you can set the function as NULL. or if you think that only both pointer is equal, the rdr is equal.

*/ int (*rdr_cmp)(void* rdr1, void* rdr2); } RPTable;

5.2 automatically free owndata We provide the function of release resource (call back function) to RPTable. If the resource is removed, the private data is automaticall removed. In original code, if we remove a resource with own data. you have to write code as following:

void *data; data = oh_get_resource_data(rpttable, res_id); release data; oh_remove_resource(rpttable, res_id);

The above code has two disadvantage: a. it is easy to forget to release the private data (For example oh_flush_rpt forgets to release private data) b. it must scan the rpttable **twice** to release resource. The new code is implemented as following: int oh_remove_resource(RPTable *table, SaHpiResourceIdT rid) { for all rdr { if(table->rdr_private_free) (*table->rdr_private_free)(rdr->rdr) release rdr

}

if (table->res_private_free) (*table->res_private_free)(res->priv); release Resource.

}

Now, it is easy to release Resource. the code is only one line: oh_remove_resource(rpttable, res_id);

5.3 int rpttable_init(RPTTable *table, void (*res_private_free)(void *), void (*rdr_private_free)(void *), int (*res_cmp)(void* res1, void* res2), int (*rdr_cmp)(void* rdr1, void* rdr2))

5.4 automatically compare owndata

5.4.1 SaHpiRptEntryT* oh_get_resource_by_owndata (RPTTable *table, void* data) { .... if (!table->res_cmp){ if (res->data == data) return res; }else { if (!(*table->res_cmp)(res->data, data)) return res; } ...

}

5.4.2 SaHpiRdrT *oh_get_rdr_by_owndata(RPTable *table, SaHpiResourceIdT rid, void* data) {

.... if (!table->rdr_cmp){ if (rdr->data == data) return rdr; }else { if (!(*table->rdr_cmp)(rdr->data, data)) return rdr; } ...

} 5.5 Example for HOWTO use

static void res_data_free(void *data) { release your private data such as memory and file description,etc.

} static void rdr_data_free(void *data) {

release your private data such as memory and file description,etc.

}

static int res_cmp(void *data1, void*data2) { struct exmple *e1 = (struct exmple *)data1; struct exmple *e2 = (struct exmple *)data2;

if (e1->field1 == e2->file2) return 0; return 1;

}

static int rdr_cmp(void *data1, void*data2) { return (data1 == data2) }

int main() {

RPTTable table;

rpttable_init(&table, &res_data_free, &rdr_data_free, &res_cmp, &rdr_cmp);

... oh_remove_resource(&table, res_id); ... res = oh_get_resouce_by_owndata(&table, data); ...

}

5.6 summary a. The new model is compatible with old model. if you use rpttable_init(&table, NULL, NULL, NULL,NULL), it is same with old model. b. you don't care about releasing your own data (memory, etc).you only provide your free function once. c The new model needn't scan rptable twice to release resource or rdr d. extent the function(you can get resource or rdr through private data)

any comment is very appreciated.

Reported by: guorj

Original Ticket: "openhpi/feature-requests/100":https://sourceforge.net/p/openhpi/feature-requests/100

mohandev2 commented 20 years ago

Original comment by: sdague